【问题标题】:Using conditional comments with RequireJS to load IE7/8 only jQuery使用带有 RequireJS 的条件注释仅加载 IE7/8 jQuery
【发布时间】:2012-08-04 12:20:18
【问题描述】:

我在我的项目中使用 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,但由于同样的问题而无法正常工作。

谢谢

【问题讨论】:

    标签: javascript jquery html requirejs require


    【解决方案1】:

    您可以在 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 的任何后续调用都应在 Javascript 中完成。

    【讨论】:

    • 非常感谢!我已经使用了它的一个变体,它工作得很好:-)
    猜你喜欢
    • 2015-09-04
    • 2012-01-25
    • 1970-01-01
    • 2014-08-06
    • 2013-02-02
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 2015-04-21
    相关资源
    最近更新 更多