[英]How to resolve the error TypeError: x is not a function for testing of the following function in Jest?
[英]How to resolve the following JS error?
我正在编写一个简单的html + js脚本,该脚本的左侧有一些图像,右侧少了一些,单击正确的元素(div的最后一个子元素)会在右侧和左侧同时添加图像(每次+5) 。 这是代码:
<html>
<head>
<style type="text/css">
img {
position:absolute;
}
div {
position:absolute;
width:500px;
height:500px;
}
#rightSide { left: 500px;
border-left: 1px solid black;
}
</style>
</head>
<body onload="generateFaces()">
<h1>Matching Game</h1>
<div id="leftSide"></div>
<div id="rightSide"></div>
<script>
var numberOfFaces = 5;
var theLeftSide = document.getElementById("leftSide");
generateFaces(theLeftSide);
function generateFaces(theLeftSide) {
for (var i = 0; i < numberOfFaces; i++) {
var img = document.createElement("IMG");
img.src="smile.png";
img.style.top = Math.floor(Math.random() * 401);
img.style.height = 100;
img.style.left = Math.floor(Math.random() * 401);
theLeftSide.appendChild(img);
};
}
leftSideImages = theLeftSide.cloneNode(true);
var theRightSide = document.getElementById("rightSide");
leftSideImages.removeChild(leftSideImages.lastChild);
theRightSide.appendChild(leftSideImages);
var theBody = document.getElementsByTagName("body")[0];
theLeftSide.lastChild.onclick= function nextLevel(event){
theLeftSide.innerHTML='';
theRightSide.innerHTML='';
event.stopPropagation();
numberOfFaces += 5;
generateFaces();
/* while(theLeftSide.firstChild)
{
theLeftSide.removeChild(theLeftSide.firstChild);
}
while(theRightSide.firstChild)
{
theRightSide.removeChild(theRightSide.firstChild);
}*/1
};
theBody.onclick = function gameOver() {
alert("Game Over!");
theBody.onclick = null;
theLeftSide.lastChild.onclick = null;
};
</script>
</script>
</body>
</html>
但是,这给了我一个错误:
Uncaught TypeError: Cannot read property 'appendChild' of undefined
我也写了删除图像的代码,这也不起作用。
尝试这个:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
/*img {
position:absolute;
}*/
div {
position:absolute;
width:500px;
height:500px;
}
#rightSide { left: 500px;
border-left: 1px solid black;
}
</style>
</head>
<body>
<h1>Matching Game</h1>
<div id="leftSide"></div>
<div id="rightSide"></div>
<script src="jquery.js"></script>
<script>
var numberOfFaces = 5;
var theLeftSide = document.getElementById("leftSide");
generateFaces();
function generateFaces() {
for (var i = 0; i < numberOfFaces; i++) {
(function(i) {
var img = document.createElement("img");
img.src="smile.png";
// img.style.top = Math.floor(Math.random() * 401);
img.style.height = 100;
// img.style.left = Math.floor(Math.random() * 401);
console.log('img');
theLeftSide.appendChild(img);
}(i));
console.log("running");
};
}
leftSideImages = theLeftSide.cloneNode(true);
var theRightSide = document.getElementById("rightSide");
leftSideImages.removeChild(leftSideImages.lastChild);
theRightSide.appendChild(leftSideImages);
var theBody = document.getElementsByTagName("body")[0];
theLeftSide.lastChild.onclick= function nextLevel(event){
theLeftSide.innerHTML='';
theRightSide.innerHTML='';
event.stopPropagation();
numberOfFaces += 5;
generateFaces();
while(theLeftSide.firstChild)
{
theLeftSide.removeChild(theLeftSide.firstChild);
}
while(theRightSide.firstChild)
{
theRightSide.removeChild(theRightSide.firstChild);
}
};
theBody.onclick = function gameOver() {
alert("Game Over!");
theBody.onclick = null;
theLeftSide.lastChild.onclick = null;
};
</script>
</body>
</html>
将图像设置为absolute
会使图像彼此重叠
更改您的功能参数名称
<html>
<head>
<style type="text/css">
img {
position:absolute;
}
div {
position:absolute;
width:500px;
height:500px;
}
#rightSide { left: 500px;
border-left: 1px solid black;
}
</style>
</head>
<body onload="generateFaces()">
<h1>Matching Game</h1>
<div id="leftSide"></div>
<div id="rightSide"></div>
<script>
var numberOfFaces = 5;
var theLeftSide = document.getElementById("leftSide");
generateFaces(theLeftSide);
function generateFaces(the_Lef_tSide) {
for (var i = 0; i < numberOfFaces; i++) {
var img = document.createElement("IMG");
img.src="smile.png";
img.style.top = Math.floor(Math.random() * 401);
img.style.height = 100;
img.style.left = Math.floor(Math.random() * 401);
theLeftSide.appendChild(img);
};
}
leftSideImages = theLeftSide.cloneNode(true);
var theRightSide = document.getElementById("rightSide");
leftSideImages.removeChild(leftSideImages.lastChild);
theRightSide.appendChild(leftSideImages);
var theBody = document.getElementsByTagName("body")[0];
theLeftSide.lastChild.onclick= function nextLevel(event){
theLeftSide.innerHTML='';
theRightSide.innerHTML='';
event.stopPropagation();
numberOfFaces += 5;
generateFaces();
/* while(theLeftSide.firstChild)
{
theLeftSide.removeChild(theLeftSide.firstChild);
}
while(theRightSide.firstChild)
{
theRightSide.removeChild(theRightSide.firstChild);
}*/1
};
theBody.onclick = function gameOver() {
alert("Game Over!");
theBody.onclick = null;
theLeftSide.lastChild.onclick = null;
};
</script>
</script>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.