[英]Javascript Uncaught TypeError: is not a function. Dynamically loaded content
I have a function onload which is just a simple console.log 我有一个函数onload,它只是一个简单的console.log
<script>
onload = function(){
console.log('Hello');
}
</script>
I call it in my body like so 我这样称呼我
<body window.onload="onload();">
which after the page loads correctly prints 页面加载正确后打印
Hello
But I am trying to make it so when I click on my it also calls this function so I tried 但是我试图做到这一点,所以当我单击它时它也会调用此函数,所以我尝试了
<th onclick()="onload();">
Doing so results in 这样做会导致
Uncaught TypeError: onload is not a function
I thought I might add that I generate my html file using a jade template so the table is dynamically loaded in (not sure if that matters) 我以为我可能会补充说,我使用玉模板生成了html文件,因此该表是动态加载的(不确定是否重要)
Thank you 谢谢
Should be: 应该:
<th onclick="onload();">
and NOT 而不是
<th onclick()="onload();">
如果表是动态加载的,则最好使用jQuery,并将事件侦听器附加到随DOM加载的div上。
$('#my_div').on('click', 'th', function(){});
The way you are attaching the listener is wrong html syntax. 您附加侦听器的方式是错误的html语法。 It should be
它应该是
<body onload="onload()">
<th onclick="onload()">
Same for every other html tags and event listeners. 其他所有html标签和事件侦听器相同。
通过将我的脚本移到底部(在加载表之后),我的问题得以解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.