无法读取属性'appendChild'为null
Cannot read property 'appendChild' of null
这是我的代码,我是java脚本的初学者。
<!doctype html>
<html>
<head>
<style>
div {position:absolute; width:500px; height:500px}
img {position:absolute}
#rightSide { left: 500px;
border-left: 1px solid black }
</style>
<script>
function generateFaces() {
var numberOfFaces = 5;
var theLeftSide = document.getElementById("leftSide");
var theRightSide = document.getElementById("rightside");
for (var i = 1; i <= numberOfFaces; i++) {
this_img = document.createElement("img");
this_img.src = "http://home.cse.ust.hk/~rossiter/mooc/matching_game/smile.png";
var topIndex = Math.floor(Math.random() * 401);
var leftIndex = Math.floor(Math.random() * 401);
this_img.style.top = topIndex + "px";
this_img.style.left = leftIndex + "px";
this_img.style.position = "absolute";
theLeftSide.appendChild(this_img);
theLeftimages = document.cloneNode(true);
theLeftimages = theLeftimages.removeChild(theLeftimages.lastChild);
theRightSide.appendChild(theLeftimages);
};
}
</script>
</head>
<body onload = "generateFaces()">
<h1>Matching Game</h1>
<p>Click on the extra smiling face on the left.</p>
<div id="leftSide"></div>
<div id="rightSide"></div>
</body>
</html>
出现问题的部分是函数生成面
function generateFaces() {
var numberOfFaces = 5;
var theLeftSide = document.getElementById("leftSide");
var theRightSide = document.getElementById("rightside");
for (var i = 1; i <= numberOfFaces; i++) {
this_img = document.createElement("img");
this_img.src = "http://home.cse.ust.hk/~rossiter/mooc/matching_game/smile.png";
var topIndex = Math.floor(Math.random() * 401);
var leftIndex = Math.floor(Math.random() * 401);
this_img.style.top = topIndex + "px";
this_img.style.left = leftIndex + "px";
this_img.style.position = "absolute";
theLeftSide.appendChild(this_img);
theLeftimages = document.cloneNode(true);
theLeftimages = theLeftimages.removeChild(theLeftimages.lastChild);
theRightSide.appendChild(theLeftimages);
};
}
当我使用RightSide.appendChild(Leftimages)追加子图像时;它显示错误为
未捕获的TypeError:无法读取null的属性"appendChild"。
并且我无法将图像克隆到右侧分区。
Javascript区分大小写。应该是右侧
var theRightSide = document.getElementById("rightSide");
由于它没有得到元素,所以它向RightSide返回Null,而Rightside显然没有appendChild函数:)
document.getElementById(...)
如果找不到具有给定id的元素,将返回null
。
// You need to change:
var theRightSide = document.getElementById("rightside");
// To:
var theRightSide = document.getElementById("rightSide");
// ^ See capital 'S'
相关文章:
- 数组在递归方法中设置为null
- 字符串在将其传递给另一个活动Android JavaScript时读取Null
- 无法获取属性'selectedIndex'的未定义引用或null引用
- Meteor方法在客户端返回null,在客户端运行的相同方法返回正确的值
- 来自文本输入null的html javascript变量
- delete在Object上效率低下,但在DOM Element's的数据属性,与null out相比
- php代码在textbox更改事件上显示null
- 相同的RegExp返回不同的结果-第一次是正确的结果,第二次是null
- 在php中提交数据时,如果某些值为null,而某些值为非null,如何进行查询
- 未捕获的类型错误:无法读取属性'addEventListener'的null chrome扩展名
- 我该如何解决这个问题?“未捕获的类型错误:无法读取 null 的属性'appendChild'”
- 无法调用方法'appendChild'为null
- Angularjs Facebook登录:无法读取属性'appendChild'为null
- 无法读取属性'appendChild'为null
- 无法获取属性'appendChild'WCF Rest中的未定义引用或null引用
- 如何解决'Cannot read property 'appendChild'null"
- 不能调用'appendChild'对于iframes为null
- appendChild null | javascript/html
- scriptexception: TypeError:不能调用null的appendChild方法
- 无法读取属性'appendChild'null"与Disqus在主干网站