我的 HTML 文件:
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" />
<script src="script.js" type="text/javascript"></script>
</head>
<body>
<form name="someForm">
<input type="button" value="Click me" onClick="myFunction()" />
</form>
<div id="result"></div>
</body>
</html>
我的 script.js 文件:
function myFunction(){
$("#result").html("BUTTON WORKS");
}
我有 Firebug,每当我单击按钮时,Firebug 都会向我抛出此错误:
ReferenceError: myFunction is not defined
我做错了什么?
最佳答案
script
不能是 HTML 中的自闭合标签(它在某些浏览器上可以工作,具体取决于文档类型,但这是不正确的)。您必须添加结束标记。
替换
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" />
与
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
由于这个错误,指向您的脚本的脚本元素可能没有被正确解析。
来自 the norm :
A script element must have both a start tag and an end tag.
Blockquote
关于javascript - 我以为我定义了我的 jQuery 函数,但我不断收到 "is not defined"错误。为什么是这样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13461723/