当我知道它存在时,找不到jQuery造成的困惑 [英] Confused by jQuery not found when I know it's there

查看:64
本文介绍了当我知道它存在时,找不到jQuery造成的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确保像这样确认对jQuery的引用是正确的.

I made sure to verify that my reference to the jQuery is correct like so.

window.onload = function() {
  console.log($(this));
};

我看到非空值.但是,当我尝试以下任何一项(上面的部分已注释掉)时,我得到了错误.

I get to see non-null value. But when I try any of the below (the part above commented out), I get the error.

未捕获的ReferenceError:未定义$

Uncaught ReferenceError: $ is not defined

//$(document).ready(function () { alert("ready"); });
$(window).onload(function () { alert("onload"); });

我很困惑如何同时定义和不定义它.经过一番谷歌搜索后,我发现了几个像这样的代码示例 ,据我所知,这不是文件中的语法

I'm confused how it can be defined and not defined at the same time. After some googling I've found several code samples like this and as far I can see, it's not a syntax in the file.

标记就是这样.

<head>
  ...
  <script src="Stuff.js" type="text/javascript"></script>
</head>
<body>
  ...
  @Scripts.Render("~/bundles/jquery")
</body>

我在Razor下的MVC.NET的默认(有效)模板中看到,它们在头部执行@Scripts.Render("~/bundles/modernizr"),然后在主体底部执行@Scripts.Render("~/bundles/jquery").我认为我可以遵循相同的模式.显然,我失败了.抱歉,不清楚.由于无知而导致的错误诊断.

I've seen in the default (and working) template for MVC.NET under Razor that they do @Scripts.Render("~/bundles/modernizr") in the head, and then @Scripts.Render("~/bundles/jquery") at the bottom of the body. I figured that I could follow the same pattern. Evidently, I failed. Sorry for being unclear. Incorrect diagnostics due to ignorance.

推荐答案

您将jQuery的script标记放在错误的位置.它是您显示的代码的script标签的之后,而应该是之前的代码.

You have your script tag for jQuery in the wrong place. It's after the script tag for the code you've shown, whereas it should be before it.

可以同时定义和未定义的原因是时间不同.这些对$的引用是在非常不同的时间进行的:

The reason it can be both defined and undefined is that the timing is different. Those references to $ are at very different times:

这个:

window.onload = function() {
  console.log($(this));
};

在页面加载周期中,真的很晚发生,当所有load事件打开时,在解析所有HTML,处理所有script标签,加载所有图像等之后window最终触发.

happens really late in the page load cycle, after all the HTML is parsed, all of the script tags processed, all the images loaded, etc., when the load event on window finally fires.

这些:

//$(document).ready(function () { alert("ready"); });
$(window).onload(function () { alert("onload"); });

立即立即,当解析器到达其所在的脚本块时,将其交给JavaScript引擎,然后JavaScript引擎运行该代码.

happen immediately when the parser reaches the script block they're in, hands off to the JavaScript engine, and the JavaScript engine runs that code.

所以这可以工作:

<script>
window.onload = function() {
    console.log($(this));
};
</script>
<script src="jquery.js"></script>

但这失败了:

<script>
$(document).ready(funtion() {
    alert("ready");
});
</script>
<script src="jquery.js"></script>

无论以下情况,该方法均有效:

This works regardless:

<script src="jquery.js"></script>
<script>
$(document).ready(funtion() {
    alert("ready");
});
</script>

这篇关于当我知道它存在时,找不到jQuery造成的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆