简体   繁体   English

Javascript Uncaught TypeError:不是函数。 动态加载的内容

[英]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.

相关问题 未捕获的TypeError:X不是函数。 使用javascript闭包时 - Uncaught TypeError: X is not a function. While using javascript closure 未捕获的TypeError:$(…)。Datatable不是函数。 但是在我调用数据表之前已经加载了jQuery - Uncaught TypeError: $(…).Datatable is not a function. But Jquery is already loaded before I call datatable javascript - Uncaught (in promise) TypeError: e.iterator is not a function。 如何修复此类错误? - javascript - Uncaught (in promise) TypeError: e.iterator is not a function. How to fix such error? Javascript Uncaught TypeError:不是函数 - Javascript Uncaught TypeError: is not a function JavaScript函数中未捕获的TypeError - Uncaught TypeError in javascript function 未捕获的TypeError:..不是javascript中的函数 - Uncaught TypeError: .. is not a function in javascript Javascript:未捕获的类型错误:不是函数 - Javascript: Uncaught TypeError: not a function 无法访问 function 中变量的属性。 未捕获的类型错误: - Unable to access a property on a variable within a function. Uncaught TypeError: 未捕获的TypeError:$(...)。data(...)。sqaveAsPDF不是函数。 剑道网格 - Uncaught TypeError: $(…).data(…).saveAsPDF is not a function. kendo Grid Uncaught TypeError formiobuilder is not a function.我做错了什么? - Uncaught TypeError formiobuilder is not a function. What did I do wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM