最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - $(document).ready(function() doesn't work - Stack Overflow

programmeradmin1浏览0评论

I have a problem with jQuery code inside a HTML code.

If I write the following code in my HTML code, it works perfectly (appear an alert saying "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:

<!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

I have a problem with jQuery code inside a HTML code.

If I write the following code in my HTML code, it works perfectly (appear an alert saying "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:

<!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

Share Improve this question edited Feb 4, 2013 at 16:16 Raulsc asked Feb 4, 2013 at 16:05 RaulscRaulsc 752 silver badges12 bronze badges 11
  • 1 Your second code sample is missing a closing > on the jquery script tag. Have you checked your browser error console? – Pointy Commented Feb 4, 2013 at 16:06
  • In both cases jquery.js is actually not loaded at all. – Ja͢ck Commented Feb 4, 2013 at 16:07
  • 1 Are you sure that jQuery is really there on your server in the same directory as the test page? – Pointy Commented Feb 4, 2013 at 16:09
  • 2 @Raulsc since you're getting only answers related to the closing bracket, you should edit your opening post with the correct code! like this, it's misleading – Samuele Mattiuzzo Commented Feb 4, 2013 at 16:10
  • 1 you wrote "type=text/javascript" instead of type="text/javascript" --> another typo? this may be your problem – Samuele Mattiuzzo Commented Feb 4, 2013 at 16:19
 |  Show 6 more ments

3 Answers 3

Reset to default 3

Your missing a closing > in the <script> tag for jQuery:

<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. If it still doesn't work, then your path to the jquery library is probably wrong.

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.

The <script> tag to include jQuery is malformed:

<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.

发布评论

评论列表(0)

  1. 暂无评论