简体   繁体   English

JavaScript函数无法与onclick一起使用

[英]JavaScript function not working with onclick

Here is the problem(if you can call it so), every time i write JS that includes functions it doesn't work. 这是问题所在(如果您可以这样称呼),每次我写包含功能的JS都行不通。 I know it's related to spaces, line-breaks or sth like that. 我知道它与空格,换行符或类似内容有关。 Maybe I don't know the rule or the syntax. 也许我不知道规则或语法。 i'm using sublime text3(if it's related). 我正在使用sublime text3(如果相关)。 And i will be thankful if you explain the reason to me so i won't be facing this problem ever again. 如果您能向我解释原因,我将不胜感激,这样我就不会再面对这个问题了。 here is a code that doesn't work on my computer 这是在我的计算机上不起作用的代码

 <script type="text/javascript"> document.getElementById("circle").onclick = function() { document.getElementById("circle").style.display = "none"; } </script> 

Are you sure that the element with the ID "circle" has the onclick function as one of its properties? 您确定ID为“ circle”的元素具有onclick函数作为其属性之一吗? you can check the property list with the dir function that is on the console. 您可以使用控制台上的dir函数检查属性列表。

console.dir(document.getElementById("circle"));

The dir is not for production though. 该目录不是用于生产的。 It does come in handy in development and debugging. 它确实在开发和调试中派上了用场。

MDN Console.dir() MDN Console.dir()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM