[英]document.getElementById not working / Display
What am i doing wrong here? 我在这里做错了什么?
Here is the code: 这是代码:
<script>
var points = 1000;
document.getElementById(pointsdisplay).innerHTML = "Points:" + points;
</script>
<p id="pointsdisplay"></p>
I want to display the points on the website. 我想在网站上显示这些要点。 I really don't understand why this isn't working.
我真的不明白为什么这不起作用。
Note that I put the script tag inside of the HTML code. 请注意,我将script标签放在HTML代码中。 Later, I will make an external js file and put everything in there.
稍后,我将制作一个外部js文件,并将所有内容放入其中。
You need to define the DOM
in HTML before the script. 您需要在脚本之前以HTML定义
DOM
。 Like this, 像这样,
<p id="pointsdisplay"></p> <script> var points = 1000; document.getElementById("pointsdisplay").innerHTML = "Points:" + points; </script>
You should be having the element id between quotations and besides, it is recommended to have the DOM element before the script tag. 您应该在引号之间使用元素ID,此外,建议在script标签之前使用DOM元素。
<p id="pointsdisplay"></p> <script> var points = 1000; document.getElementById('pointsdisplay').innerHTML = "Points:" + points; </script>
I think I see two problems here. 我想我在这里看到两个问题。
The first is that the document.getElementById needs to be given a String. 首先是需要给document.getElementById一个字符串。 You could use:
document.getElementById("pointsDisplay")
. 您可以使用:
document.getElementById("pointsDisplay")
。
Secondly, I think you will need to put the script underneath the p. 其次,我认为您需要将脚本放在p的下面。 The browser will execute the script as soon as it hits the script tag.
浏览器在点击脚本标签后将立即执行脚本。
There are two mistakes here, 这里有两个错误
"
(Double Quotes) "
(双引号) dom
element is created, else it may execute before the actual dom
is created. dom
元素创建后放置脚本标记,否则它可能在创建实际dom
之前执行。 Even if you keep js code in a separate file, make sure you write it after your dom
, just before the body
tag ends. dom
之后, body
标签结束之前编写它。 So your code should be like, 所以你的代码应该像
<p id="pointsdisplay"></p>
<script>
var points = 1000;
document.getElementById('pointsdisplay').innerHTML = "Points:" + points;
</script>
The id name should be in string and the HTML tag should loaded first ID名称应为字符串,并且应首先加载HTML标记
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!--you have two options --> <p id="pointsdisplay"></p> <!--Html tag should be loaded first --> <script> var points = 1000; // the id name should be in string document.getElementById("pointsdisplay").innerHTML = "Points:" + points; </script> <!-- second option jquery ready function, so that would wait for the document to be loaded then execute the script --> <script> var points = 1000; $(document).ready(function(){ document.getElementById("pointsdisplay1").innerHTML = "Points:" + points; }); </script> <p id="pointsdisplay1"></p>
All of the other answers here are correct however I would like to point out that if you use the DOMContentLoaded event the script will wait until the DOM has loaded before executing. 这里的所有其他答案都是正确的,但是我想指出的是,如果您使用DOMContentLoaded事件,则脚本将等到DOM加载后再执行。
Eg. 例如。
<script> document.addEventListener("DOMContentLoaded", function(event) { var points = 1000; document.getElementById("pointsdisplay").innerHTML = "Points:" + points; }); </script> <p id="pointsdisplay"></p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.