[英]jQuery code doesn't work when put within head tag
I have a jQuery code that works when I insert it inside a script tag within the body. 我有一个jQuery代码,当我将其插入到体内的script标签内时可以使用。 But it doesn't work when I insert it in the head tag or include it as a js file.
但是,当我将其插入到head标记中或将其作为js文件包含在内时,它不起作用。
Judging from your description you most probably forgot to put your script into a $(document).ready
function: 从您的描述来看,您很可能忘记了将脚本放入
$(document).ready
函数中:
$(function() {
// put your code here
});
Then you can place this wherever you want on the page ( head
, body
, external script, ...). 然后,您可以将其放置在页面上的任何位置(
head
, body
,external script等)。
Any script code put inside a head is not supposed to execute automatically. 放在头中的任何脚本代码都不应自动执行。 Unless called by a event listener.
除非由事件侦听器调用。 So if you have put your code in the head section its just a piece of functionality waiting to be executed on a call.
因此,如果您将代码放在头部分中,它只是一项功能等待在调用时执行。 This code can be called as mentioned on a document.ready( ) event handler or any other event call from the user.
可以按document.ready()事件处理程序中所述的方式调用此代码,也可以从用户调用任何其他事件。
try these format 试试这些格式
<html>
<head>
<script>
$(document).ready(function() {
$('#arrow img').click(function() {
transferEmp('test');
});
});
</script>
<body>
<!-- other tags -->
<script>
$(document).ready(function() {
$('#arrow img').click(function() {
transferEmp('test');
});
});
</script>
<script src="test.js" type="text/javascript"></script>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.