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

hch*_*rge 3 html javascript jquery require requirejs

我在我的项目中使用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]-->
Run Code Online (Sandbox Code Playgroud)

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

谢谢

chr*_*isf 16

您可以在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> 
Run Code Online (Sandbox Code Playgroud)

然后在里面main.js,你可以使用:

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

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