我在页面的结束标记之前加载了 jQuery。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
我的大部分js都在这下面加载
<script src="js/script.js" type="text/javascript"></script>
效果很好。 我需要在 head 标记中运行一些广告代码。 这通常工作正常但是我想添加一个响应异常。 例如
var mob = 0;
if(jQuery(window).width() < 727) {
mob = 1;
}
if(mob) {console.log('run special mob related code');}
else {console.log('run special desktop related code');}
因为我的代码在我收到错误 ReferenceError: jQuery is not defined. 有没有解决的办法? 广告代码只有在头部时才会运行,但我无法真正将 jquery 移动到头部,因为我在多个站点上都有它,并且可能会影响其他插件的运行方式?
最佳答案
为什么不直接使用这样的东西:
if (document.documentElement.clientWidth < 727) {
mob = 1;
}
关于javascript - 加载 jQuery 后运行 jQuery 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18093090/