我m trying to create an element(image) and then set a loop that would produce 5 images in one div(its id="leftSide"). Here
是我的代码:
<script>
var number_of_faces = 0;
var theLeftSide = document.getElementById("leftSide");
function generate_faces() {
for (number_of_faces = 0; number_of_faces < 6; number_of_faces++) {
newFace = document.createElement("img");
newFace.setAttribute(
"src",
"http://home.cse.ust.hk/~rossiter/mooc/matching_game/smile.png"
);
document.getElementById("leftSide").appendChild("img");
}
}
</script>
Body onload必须调用该函数,但没有任何反应。 Div仍然是空的&#34;
答案 0 :(得分:2)
试用此代码
"goetas/xsd2php": "2.0.x-dev#fdc2ab0bb3f2b3ab796ca567cf8c0f3446a7ea3a"
答案 1 :(得分:1)
使用document.getElementById("leftSide").appendChild("img");
document.getElementById("leftSide").appendChild(newFace);
答案 2 :(得分:1)
document.getElementById("leftSide").appendChild(newFace);
< 5
循环5次。