我在我的项目中使用Require JS,它正在加载jQuery和其他一些与整个站点和所有浏览器相关的JavaScript文件。
但是,我需要在Internet Explorer 7和Internet上使用一些条件jQuery。 8,我已经尝试将它放在页面的头部并且脚本似乎无法找到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因为同样的问题而无效。
由于
答案 0 :(得分: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>
然后在main.js
内,你可以使用:
if ($('html.lt-ie9').size()) {
require(['/Scripts/ie'], function(ieScript) {
// ... do stuff
});
}
您无法使用您所描述的方法,因为require.js会查找单个data-main
属性来指定“入口点” - 对require
的任何后续调用都应在Javascript中完成