[英]Why does not this code work?
This code should work.. But it doesn't and I get an error message that says: 这段代码应该工作..但它没有,我得到一条错误信息,上面写着:
Uncaught TypeError: Cannot set property 'innerHTML' of null
It is not the first time I've gotten this error and I still don't understand it... 这不是我第一次遇到这个错误,我仍然不明白......
HTML: HTML:
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="mycss.css">
<script src="myjavascript2.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<button onclick="myFunction()">Play</button>
<p id="test"></p>
</body>
</html>
javascript: JavaScript的:
var things = ['rock','paper','scissors'];
function myFunction() {
var i = Math.floor(Math.random()*things.length));
document.getElementById("test"+(i+1)).innerHTML=things[i];
}
它看起来像你有任何元素与像test1
, test2
等ids。
The element referenced by your code does not exist in the DOM: 您的代码引用的元素在DOM中不存在:
document.getElementById("test"+(i+1))
You have in your HTML: 你有HTML:
<p id="test"></p>
Your code is looking for "test3" 你的代码正在寻找“test3”
If you want the single element to change the text, have this as your JavaScript: 如果您希望单个元素更改文本,请将其作为您的JavaScript:
document.getElementById("test").innerHTML=things[i];
If you want it go go into multiple elements, then in your HTML replace <p id="test"></p>
with: 如果你想要它进入多个元素,然后在你的HTML替换<p id="test"></p>
:
<p id="test1"></p>
<p id="test2"></p>
<p id="test3"></p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.