Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么jquery/javascript代码不';当我在带有src属性的同一个脚本标记中使用它时,它不起作用_Javascript_Jquery_Html - Fatal编程技术网

为什么jquery/javascript代码不';当我在带有src属性的同一个脚本标记中使用它时,它不起作用

为什么jquery/javascript代码不';当我在带有src属性的同一个脚本标记中使用它时,它不起作用,javascript,jquery,html,Javascript,Jquery,Html,可能重复: 我在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

可能重复:

我在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>

在此处插入标题
$(文档).ready(函数(){
警报(“ABC”);
});
这是我的索引页

jquery在这里不起作用,这意味着我在添加警报后看不到任何效果

但是当我在下面的标签中单独添加

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

在此处插入标题
$(文档).ready(函数(){
警报(“ABC”);
});
如果你点击我,我就会消失


因此,如果有人解释的话?

这是因为,您提供了带有javascript的src标记

src文件内容将扩展为
(开始和结束)标记的内容

如果需要,您可以使用firebug进行检查。

来自W3学校:

注意:如果存在“src”属性,则元素必须为空


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

第一个标记所做的是告诉浏览器脚本的内容位于外部文件中。它将忽略实际标记中的任何脚本,以支持文件的内容


您需要使用单独的标签。

我不知道。但是如果您在标记内的“src”中指定的源是在线源

如果保存,请转到该源并复制脚本并将其保存到本地。然后

分配“src”你的本地一个然后它将工作。即

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="../polin/jquery_library.js">
function fn()
{alert("pp");}
</script>
<script type="text/javascript">
$(document).ready(function(){
alert("ABC");
});
function fn()
{alert("oo");}
</script>
</script>
 </head>

<body onload="fn()">
<p>THIS IS MY INDEX PAGE.</p>
</body>
</html>

在此处插入标题
函数fn()
{alert(“pp”);}
$(文档).ready(函数(){
警报(“ABC”);
});
函数fn()
{alert(“oo”);}
这是我的索引页


src=“../polin/jquery_library.js”此文件位于保存jquery源脚本的本地文件中。而且很有效

谢谢你的回答和评论。即使是谷歌也无法在如此短的时间内给出如此多的准确答案。在这种情况下,这是错误的。“必须为空”为false,它可以包含内容,但不会执行。请小心引用。他们不知道他们的准确性。在这种情况下,元素不必“为空”,脚本中的内容将被忽略。Mozilla文档是一个更加完整和准确的资源。我会记住这一点,我使用它是因为这是我搜索源代码的第一个结果(它们似乎定位正确)。