jquery绑定按钮点击事件失败
test.html:
<html>
<head>
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript" src="test.js"></script>
</head>
<body>
<input id="but2" type="button" value="2"/>
</body>
</html>
jquery-1.4.2.js 是从 http://jquery.com/
test.js 下载的:
var fn=function(){
alert('success!');
};
$('#but2').click(fn);
当点击按钮时,没有任何反应。我调试了很长时间但没有找到根本原因。请帮忙。
test.html:
<html>
<head>
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript" src="test.js"></script>
</head>
<body>
<input id="but2" type="button" value="2"/>
</body>
</html>
jquery-1.4.2.js is downloaded from http://jquery.com/
test.js:
var fn=function(){
alert('success!');
};
$('#but2').click(fn);
When clicked the button, nothing happened. I debugged for very long time but didn't find the root cause. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其包装起来,以便在文档加载之前代码不会运行。
尝试一下: http://jsfiddle.net/ApDKU/
执行:
...与...相同,
导致内部代码仅在
标签加载完成后运行。
按照您的方式,将
click
处理程序附加到#but2
的代码在#but2
之前运行加载到页面上。Wrap it such that the code doesn't run until the document has loaded.
Try it out: http://jsfiddle.net/ApDKU/
Doing:
...is the same as
...which cause the code inside to run only after the
<body>
tag has finished loading.The way you had it, the code that attached the
click
handler to#but2
was running before#but2
had loaded onto the page.