简体   繁体   English

条件注释替代加载jQuery

[英]Conditional comments alternative to load jQuery

Because jQuery 2+ doesn't support IE 8, I'll need to make use of a previous version: jQuery 1.9. 因为jQuery 2+不支持IE 8,所以我需要使用以前的版本:jQuery 1.9。

Is there a way to include jQuery 1.9 when using IE <= 8 and jQuery 2+ when using IE > 8. I tried it with Conditional comments: 有没有一种方法可以在使用IE <= 8时包含jQuery 1.9,在使用IE> 8时包含jQuery 2+。我尝试使用条件注释:

<!--[if lt IE 9]><!--><script> .. googleapis jquery 1.9 .. </script><!--<![endif]-->
<!--[if gte IE 9]><!--><script> .. googleapis jquery 2+ .. </script><!--<![endif]-->

The problem is that It is not working and I found some information about it not being supported anymore: http://msdn.microsoft.com/en-us/library/ie/hh801214(v=vs.85).aspx 问题是它无法正常工作,我发现一些不再受支持的信息: http : //msdn.microsoft.com/zh-cn/library/ie/hh801214( v= vs.85).aspx

Is there another way? 还有另一种方法吗?

You want the IE8 version to be in a commented-out block, but the non-IE9 version not to be: 您希望IE8版本被注释掉,但非IE9版本则不要:

<!--[if lt IE 9]> <script> .. googleapis jquery 1.9 .. </script> <![endif]-->
<![if !IE|gte IE 9]> <script> .. googleapis jquery 2+ .. </script> <![endif]>

Live Example with alerts 带有警报的实时示例

IE8 recognizes conditional comments, but other browsers don't (including newer IEs). IE8可以识别条件注释,而其他浏览器则不能(包括较新的IE)。

You also probably want to have !IE in the second one, just for completeness, as above. 如上所述,您可能还想在第二个中使用!IE ,以确保完整性。

This will serve a different version of jQuery to Internet Explorer versions 6-8: 这将提供与Internet Explorer 6-8版本不同的jQuery版本:

<!--[if lt IE 9]>
    <script src="jquery-1.9.0.js"></script>
<![endif]-->
<!--[if gte IE 9]><!-->
    <script src="jquery-2.0.0.js"></script>
<!--<![endif]-->

or the somewhat easier to read (but functionally identical): 或更容易阅读(但功能相同):

<!--[if lt IE 9]>  
    <script src="jquery-1.9.0.js"></script>  
<![endif]-->  
<!--[if (gte IE 9) | (!IE)]><!-->  
    <script src="jquery-2.0.0.js"></script>  
<!--<![endif]--> 

Bravo to TJ Crowder (above) for his answer. 致敬TJ Crowder(上方),谢谢他的回答。 This one racked my brain, I was having some trouble as IE8 was loading both JQuery files at times or the script I wrote would not allow JQuery to fire afterward due to the way the document was loading. 这让我大吃一惊,由于IE8有时会同时加载两个JQuery文件,或者由于文档加载的方式,我编写的脚本不允许JQuery之后启动,因此我遇到了麻烦。 One addition to the above answer, I actually had to reverse the order of the two conditionals in order to ensure more than one copy of JQuery was not loading in IE8. 除了上述答案外,我实际上还必须颠倒两个条件的顺序,以确保IE8中不会加载多个JQuery副本。

<![if !IE|gte IE 9]> <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script> <![endif]> <!--[if lt IE 9]> <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> <![endif]-->

You could check the UserAgentString on server code, and include one or another depending on it. 您可以检查服务器代码上的UserAgentString,并根据它包含一个或另一个。 Here is a list of Internet Explorer User Agents: http://www.useragentstring.com/pages/Internet%20Explorer/ 以下是Internet Explorer用户代理的列表: http : //www.useragentstring.com/pages/Internet%20Explorer/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM