这段代码应该可以工作..但它没有,我收到一条错误消息,上面写着:
Uncaught TypeError: Cannot set property 'innerHTML' of null这不是我第一次收到这个错误了,我仍然不理解它。
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:
var things = ['rock','paper','scissors'];
function myFunction() {
var i = Math.floor(Math.random()*things.length));
document.getElementById("test"+(i+1)).innerHTML=things[i];
}发布于 2014-02-14 03:14:22
代码引用的元素在DOM中不存在:
document.getElementById("test"+(i+1))您的HTML中有:
<p id="test"></p>您的代码正在查找"test3“
https://stackoverflow.com/questions/21763402
复制相似问题