【问题标题】:Can I place IE conditionals in functions.php - WordPress我可以在functions.php中放置IE条件吗 - WordPress
【发布时间】:2014-11-22 19:02:50
【问题描述】:

我们被教导将以下脚本放在<head> 部分中,以 IE 9 为条件。

<!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

在 WordPress 中,我倾向于使用 wp_enqueue_script() 将脚本挂接到 wp_head()(默认)或 wp_footer()。我的问题是:

(1) 我们可以在 WordPress 主题中的 functions.php 中实现类似以下的内容吗?

function project_scripts(){?>
  <!--[if lt IE 9]>
  <?php
     wp_enqueue_script( 'IE-scripts', 'http://html5shiv.googlecode.com/svn/trunk/html5.js' );
  ?>
  <![endif]-->
<?php
}
add_action( 'wp_enqueue_scripts', 'project_scripts' );

(2)如果可以,我可以不这样做吗:

function project_scripts(){
  echo '<!--[if lt IE 9]>';
     wp_enqueue_script( 'IE-scripts', 'http://html5shiv.googlecode.com/svn/trunk/html5.js' );
  echo '<![endif]-->';
}
add_action( 'wp_enqueue_scripts', 'project_scripts' );

【问题讨论】:

    标签: php css wordpress internet-explorer-9


    【解决方案1】:

    没有。 JavaScript 不能包含 IE 条件。

    这是因为 IE 条件是 HTML 注释节点,就像普通的 HTML 标记在 JavaScript 源代码中没有意义一样,这样的 cmets 会导致 invalid JavaScript。

    可能值得研究像Modernizr 这样利用特征检测的方法。或者,可以使用 IE 条件将类添加到稍后可以检测到的元素,as shown here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-16
      • 1970-01-01
      • 2015-10-29
      • 2018-03-10
      • 1970-01-01
      • 1970-01-01
      • 2018-07-07
      相关资源
      最近更新 更多