为什么当我在src属性的相同脚本标签中使用jquery/javascript代码时,jquery/javascript代码不起作用

Abu*_*kar 0 html javascript jquery

可能重复:
带有src和内容的脚本标记是什么意思?

我在我的html页面中使用了以下内容

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="resources/scripts/jquery-1.8.2.min.js">

$(document).ready(function(){
  alert("ABC");
});
</script>
</head>

<body>
<p>THIS IS MY INDEX PAGE.</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

jquery在这里不起作用,这意味着我在其中发出警报后看不到它的任何影响.

但是,当我<script>按照下面的标签放入单独的标签时,

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="resources/scripts/jquery-1.8.2.min.js">
</script>
<script>
$(document).ready(function(){
  alert("ABC");
});
</script>
</head>

<body>
<p>If you click on me, I will disappear.</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

所以,如果有人解释?

san*_*nte 6

来自w3schools:

http://www.w3schools.com/tags/tag_script.asp

"注意:如果存在"src"属性,则该元素必须为空."

当您使用脚本标记时,您可以使用src属性或标记内部提供源代码,但不能同时提供这两者.

  • https://developer.mozilla.org/en-US/docs/HTML/Element/script w3schools在这种情况下是错误的."必须为空"是错误的,它可以包含内容,但不会执行. (4认同)