简体   繁体   English

$(document).ready(function()不起作用

[英]$(document).ready(function() doesn't work

I have a problem with jQuery code inside a HTML code. 我在HTML代码内的jQuery代码有问题。

If I write the following code in my HTML code, it works perfectly (appear an alert saying "hello world"): 如果我在HTML代码中编写以下代码,则该代码可以正常运行(出现警告,提示“ hello world”):

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery.js"> </script>
<script>
      alert("hello world");
</script>
</head>
</html>

But when I write the following code (with $(document).ready function) it doesn't work and I don't know why: 但是,当我编写以下代码(使用$(document).ready函数)时,它不起作用,我也不知道为什么:

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery.js"> </script>
<script>
$(document).ready(function() {
alert("hello world");
});
</script>
</head>
</html>

Could anybody tell me what's going on here? 有人可以告诉我这是怎么回事吗?

Thanks in advance. 提前致谢。

edit: sorry about the missing bracket, it was just a typo 编辑:很抱歉缺少括号,这只是一个错字

Your missing a closing > in the <script> tag for jQuery: 您在jQuery的<script>标记中缺少结束符>

<script type="text/javascript" src="jquery.js"> </script>
                                              ^
 missing bracket here ------------------------|

You're missing quotes and the closing > of the start tag in your script tag, which means it's broken so the script isn't loading. 您在脚本标签中缺少引号和开始标签的结束> ,这意味着它已损坏,因此脚本无法加载。

<script type="text/javascript" src="jquery.js"> </script>

Your edited question shows the corrected script tag. 您编辑的问题将显示更正的script标记。 If it still doesn't work, then your path to the jquery library is probably wrong. 如果仍然无法使用,则您通往jquery库的路径可能是错误的。

When coding, you should keep your browser's developer console open. 编码时,应保持浏览器的开发人员控制台处于打开状态。 It'll show errors that are happening. 它将显示正在发生的错误。 There's probably a ReferenceError stating that $ is undefined . 可能有一个ReferenceError指出$undefined

The <script> tag to include jQuery is malformed: 包含jQuery的<script>标记格式错误:

<script type=text/javascript src="jquery.js" </script>

It should look like this: 它看起来应该像这样:

<script type="text/javascript" src="jquery.js"></script>

This is assuming that the jQuery library is in a file named "jquery.js" within the same directory as your HTML page. 假设jQuery库位于与HTML页面相同目录中的名为“ jquery.js”的文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM