繁体   English   中英

使用RequireJS的条件注释仅加载IE7 / 8 jQuery

[英]Using conditional comments with RequireJS to load IE7/8 only jQuery

我在我的项目中使用Require JS,它正在加载jQuery和其他一些与整个站点和所有浏览器相关的JavaScript文件。

但是,我需要在Internet Explorer 7和8上使用一些条件jQuery,我已经尝试将它放在页面的头部并且脚本似乎无法找到jQuery,我假设这是因为它是在jQuery之前加载。

 <script data-main="/@ViewBag.ScriptsFolder/main" src="/scripts/require.js" type="text/javascript"></script>
    <!--[if (gte IE 6)&(lte IE 8)]>
        <script type="text/javascript" src="/Scripts/ie.js"></script>
    <![endif]-->

有没有办法纠正这个? 我也试图以这种方式加载Selectivizr因为同样的问题而无法正常工作。

谢谢

您可以在html元素上使用条件IE特定的类,然后检查它并在现有main脚本中加载IE依赖项。 所以,文档的顶部看起来像:

<!--[if lt IE 7]> <html class="ie lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>    <html class="ie lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>    <html class="ie lt-ie9"> <![endif]-->
<!--[if gt IE 8]> <html class="ie"> <![endif]-->
<html> 

然后在main.js ,您可以使用:

if ($('html.lt-ie9').size()) {
    require(['/Scripts/ie'], function(ieScript) {
        // ... do stuff
    });
}

您不能使用您所描述的方法,因为require.js查找单个data-main属性来指定“入口点” - 任何后续的require调用require应该在Javascript中完成。

暂无
暂无

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

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