jQuery不适用于新手

jQuery not working for a newbie

本文关键字:新手 适用于 不适用 jQuery      更新时间:2024-06-12

我在这里使用bob-tabors-jQuery视频教程已经有很长一段时间了,我无法让jQuery工作。

这是我的html

<!DOCTYPE html>
<html>
<head>
 <script type="text/javascript" src="jquery.js"></script>  
<script type="text/javascript" src="script.js"></script>   
  <link rel="stylesheet" type="text/css" href="main.css" />
  <title>14. Getting Started with jQuery</title>
</head>
<body>
  <h1 id="title">14. Getting Started with jQuery</h1>
  <p id="first">
    <span>Now is the time for all good men to come to 
          the aid of their country.</span>
  </p>
  <p id="second">
    <strong>In the end these things matter most: 
       How well did you love? How fully did you live?</strong> 
  </p>
  <p id="third">
    <a id="myAnchor" href="http://www.learnvisualstudio.net">Visit my website</a>
  </p>
</body>
</html>

这是我的js

jQuery(document).ready(function() {
alert("thsdjasl");
}

但在html之后,就没有警报了。

您缺少就绪函数的右括号:

jQuery(document).ready(function() {
   alert("thsdjasl");
});

如果没有这个,你的警报将不会运行,你将得到以下错误:

未捕获的SyntaxError:意外的令牌}

您缺少一个右括号和分号。理想情况下,您应该为文档准备好的部分使用"$"命名法:

$(document).ready(function() {
    alert("thsdjasl");

});

最后,您可以通过在浏览器中打开开发人员工具(Chrome很好),转到javascript源选项卡,查找弹出的错误和警告,自己解决这个问题。

确保jquery.js文件位于根目录中。并使用这个:

$(document).ready(function(){
    console.log('bla bla ');
    alert('bla');
});