所以,我的任务是将一个div传递给一个函数,然后返回一个函数,该函数将创建一个新的div,分配一个类(在CSS中定义)并添加到div最初传入该函数。通过从JavaScript中的函数返回函数来修改div
更具体地说,addDivTo()
函数接收一个div id并返回一个函数。被返回的函数接收一个类作为参数,在这个函数中应创建新的div,应用该类并创建随机位置以放置在原始div中。
我花了一些时间来解决这个问题,我想我已经正确地构建了addDivTo()
,因为我将该函数接收回变量,但是在尝试将该类传递给返回函数时没有发生任何事情。
我很抱歉,我在我的代码中有一堆评论,试图保持我试图做的事情,但我显然没有做正确的事,因为没有任何事情发生! 如果有人能指出我正确的方向或解释我做错了什么,我将不胜感激。
<!doctype html>
<html>
<head>
<title> Functions: Returning a function from a function </title>
<meta charset="utf-8">
<style>
html, body, div#container {
width: 100%;
height: 100%;
margin: 0px;
padding: 0px;
}
div#container1, div#container2 {
position: relative;
height: 300px;
border: 1px solid black;
}
.red {
position: absolute;
width: 150px;
height: 150px;
background-color: red;
}
.blue {
position: absolute;
width: 175px;
height: 175px;
background-color: lightblue;
}
.circle {
width:100px;
height:100px;
border-radius:50px;
font-size:20px;
color:#fff;
line-height:100px;
text-align:center;
background:#000
}
.squared {
border: 2x solid black;
}
</style>
<script>
window.onload = init;
function init() {
//Pass value for div container into addDivTo()
//addDivTo() will return a function to the variable passed to it
//when calling function returned from addDivTo pass in name of
//class the div should have
var containerOne = document.getElementById("container1");
var containerTwo = document.getElementById("container2");
var colorOne = getComputedStyle(document.body, "red");
var colorTwo = getComputedStyle(document.body, "blue");
var shapeSquare = getComputedStyle(document.body, "square");
var shapeCircle = getComputedStyle(document.body, "circle");
//call addDivTo() calling containers
//return function will be contained in the variables
var newDiv1 = addDivTo(containerOne);
var newDiv2 = addDivTo(containerTwo);
//pass class to function which should return the new div and class
//and append to original container passed to addDivTo
var addColor = newDiv1(colorOne);
containerOne.appendChild(addColor);
var addShape = newDiv1(shapeSquare);
containerOne.appendChild(addShape);
var addColor2 = newDiv2(colorTwo);
containerTwo.appendChild(addColor2);
var addShape2 = newDiv2(shapeCircle);
container2.appendChild(addShape2);
}
function addDivTo (containerIn) {
//Takes container id and returns a function when called
//Return function takes class, create new div element and add to div
//passed in the call addDivTo
return function(classToAdd) {
var divIn = containerIn;
var classIn = classToAdd;
var newDiv = document.createElement('div');
newDiv.id = "funcDiv";
newDiv.style.left = Math.floor(Math.random() * (newDiv.offsetWidth
= 175)) + "px";
newDiv.style.right = Math.floor(Math.random() *
(newDiv.offsetHeight = 175)) + "px";
newDiv.className = classIn;
return newDiv;
};
}
</script>
</head>
<body>
<div id="container1"></div>
<div id="container2"></div>
</body>
</html>
在声明中:var funcAddToContainerOne = addDivTo(containerOneId)我应该在那里调用元素名称还是与funcAddToContainerOne = addDivTo(containerOne)相同,其中document.getElementById(“container1)被分配给containerOne变量? – TRogers 2015-04-03 18:36:19
如果您需要在该插入函数之外操作“container1”,则传递该元素。如果您不需要在此函数之外进行操作,则传递该id并让该函数找到该元素。 – Jasen 2015-04-03 18:39:53
在某些情况下,您将永远不会确切知道某个元素的Id是什么,但会将其作为一个对象引用 - 然后您将传递该元素本身。 – Jasen 2015-04-03 18:42:24