简体   繁体   English

卡在onclick和document.GetElementById

[英]Stuck with onclick and document.GetElementById

I just began JavaScript and I've been stuck for a few hours now with an onclick which is not working, or maybe it's the document.getElementById . 我刚开始使用JavaScript,但由于onclick无效,或者可能是document.getElementById ,我已经呆了几个小时。 What I want to do is hide the div when I click on it. 我要做的是单击div时将其隐藏。

If anyone can explain me what I'm doing wrong I would be grateful! 如果有人可以解释我在做什么,我将不胜感激!

 function closing() { var closecook = document.getElementById("cookies"); closecook.style.display = "none"; } 
 #cookies{ display: block; } 
 <div id="cookies" onclick="closing()"> Our website is using cookies, click here to close this message. </div> 

Here's my relevant HTML markup: 这是我相关的HTML标记:

<body>
<div id="cookies" onclick="closing()">
    Our website is using cookies, click here to close this message.
</div>
</body>
<script type="text/javascript" src="functions.js"></script>

Thanks. 谢谢。

  1. Place your "script" block at the end of your body, not outside of it! 将您的“脚本”块放在身体的末端,而不是在身体的外面!
  2. alter you the function "closing" to something like this: 将您的函数“关闭”更改为如下所示:

    function closing() { var closecook = document.getElementById("cookies"); console.log(closecook); closecook.style.display = "none"; }

  3. In your browser: open the console by hitting "F12" or right-click anywhere in your browser and select "inspect element" then choose "console". 在浏览器中:按下“ F12”打开控制台,或在浏览器中的任意位置单击鼠标右键,然后选择“检查元素”,然后选择“控制台”。

  4. Now execute your function by clicking on the div. 现在,通过单击div执行您的功能。

If you see a "null" in the console: the problem comes from "document.getElementById("");". 如果您在控制台中看到“空”:问题来自“ document.getElementById(“”);“。

If you do not see anything pop up in the console, your javascript file is not loaded properly. 如果您没有在控制台中看到任何弹出消息,则说明您的javascript文件未正确加载。 Make sure there is no typo in the file name! 确保文件名中没有错字! (Linux is case-sensitiv!). (Linux区分大小写!)。

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

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