Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在wordpress functions.php中加载适用于IE的jquery v.1和适用于现代浏览器的jquery v.2_Jquery_Wordpress - Fatal编程技术网

在wordpress functions.php中加载适用于IE的jquery v.1和适用于现代浏览器的jquery v.2

在wordpress functions.php中加载适用于IE的jquery v.1和适用于现代浏览器的jquery v.2,jquery,wordpress,Jquery,Wordpress,我想向我的网站添加IE8支持,但我的网站使用了IE8不支持的jquery v.2。所以我想为IE8用户加载jQueryV.1,但这不起作用,有人能看到我代码中的错误吗 当我访问IE8中的网站时,我会收到数百个错误,因为它只加载了v.2。另一方面,safari没有加载v.1,因此这里只有IE有问题 echo '<!--[if lt IE 9]>'; wp_deregister_script( 'jquery' ); wp_register_script(

我想向我的网站添加IE8支持,但我的网站使用了IE8不支持的jquery v.2。所以我想为IE8用户加载jQueryV.1,但这不起作用,有人能看到我代码中的错误吗

当我访问IE8中的网站时,我会收到数百个错误,因为它只加载了v.2。另一方面,safari没有加载v.1,因此这里只有IE有问题

echo '<!--[if lt IE 9]>';
        wp_deregister_script( 'jquery' );
        wp_register_script( 'jquery', get_stylesheet_directory_uri() . "/_plugins/jquery/jquery-1.11.3.min.js", false, false, false );
        wp_enqueue_script( 'jquery' );
    echo '<![endif]-->';
    echo '<!--[if gte IE 9]><!-->';
        wp_deregister_script( 'jquery' );
        wp_register_script( 'jquery', get_stylesheet_directory_uri() . "/_plugins/jquery/jquery-2.1.4.min.js", false, false, false );
        wp_enqueue_script( 'jquery' );
    echo '<!--<![endif]-->';

如果查看生成的脚本,您会发现:

<!--[if lt IE 9]>
<![endif]-->
<!--[if gte IE 9]><!-->
<!--<![endif]-->
脚本只在代码中注册,加载脚本的脚本标记稍后编写。如果立即写入脚本标记,则无法注销脚本

通过调整方法,您应该能够使用它来包装编写的脚本标记:

wp_register_script('jquery1', get_stylesheet_directory_uri() . "/_plugins/jquery/jquery-1.11.3.min.js", false, false, false);
wp_register_script('jquery2', get_stylesheet_directory_uri() . "/_plugins/jquery/jquery-2.1.4.min.js", false, false, false);

add_filter('script_loader_tag', function($tag, $handle) {
  if ($handle === 'jquery1') {
    $tag = "<!--[if lt IE 9]>$tag<![endif]-->";
  }
  if ($handle === 'jquery2') {
    $tag = "<!--[if gte IE 9]><!-->$tag<!--<![endif]-->";
  }
  return $tag;
}, 10, 2);

您可以复制并粘贴您得到的错误吗?另外,请注释掉该代码,并将其复制并粘贴到header.php中,如果错误仍然存在,请告诉我:我认为您将很难编写同时支持这两种功能的jQuery代码。如果需要旧版浏览器支持,请使用较旧版本的jQuery。也就是说,出现问题的原因是您尝试使用排队脚本的方式。它不会显示在你的IE标签中。很抱歉,我以为它可以工作,但现在我看到,通过你的脚本,它在两种现代浏览器+IE上都加载了v.1。@HenningFischer:有条件的注释会在页面中结束吗?