admin管理员组文章数量:1287856
My question is:
Is that possible to add the same element without rewriting the same variable.
I am creating a slider, and i need to append a div
with a class slide-el
into block slider
.
Here is a part of code
var body, html, sliderBody, btnLeft, btnRight, i, parts, vHeight, vWidth;
//Variable definitions
var i = 0,
parts = 3,
//Main html elements
body = document.body,
html = document.element,
//viewport Height and Width
vHeight = window.innerHeight,
vWidth = window.innerWidth,
sliderBody = _id("slider"),
btnLeft = _id("btn-left"),
btnRight = _id("btn-right"),
urls = [".jpg",
".jpg",
".jpg",
".jpg"];
slide = _createEl("div");
slide.className += "slide-el";
function _id(el){
return document.getElementById(""+ el +"");
}
function _createEl(el){
return document.createElement(""+ el +"");
}
window.onload = function(){
slideLayout();
}
function slideLayout(){
for(var i=0; i < urls.length; i++){
sliderBody.appendChild(slide);
}
}
The problem is that I can't append the same element that many times. It just creates one element instead of 4.
For you to understand better I made a fiddle:
/
My question is:
Is that possible to add the same element without rewriting the same variable.
I am creating a slider, and i need to append a div
with a class slide-el
into block slider
.
Here is a part of code
var body, html, sliderBody, btnLeft, btnRight, i, parts, vHeight, vWidth;
//Variable definitions
var i = 0,
parts = 3,
//Main html elements
body = document.body,
html = document.element,
//viewport Height and Width
vHeight = window.innerHeight,
vWidth = window.innerWidth,
sliderBody = _id("slider"),
btnLeft = _id("btn-left"),
btnRight = _id("btn-right"),
urls = ["http://www.wallpapereast./static/images/pier_1080.jpg",
"http://www.wallpapereast./static/images/pier_1080.jpg",
"http://www.wallpapereast./static/images/pier_1080.jpg",
"http://www.wallpapereast./static/images/pier_1080.jpg"];
slide = _createEl("div");
slide.className += "slide-el";
function _id(el){
return document.getElementById(""+ el +"");
}
function _createEl(el){
return document.createElement(""+ el +"");
}
window.onload = function(){
slideLayout();
}
function slideLayout(){
for(var i=0; i < urls.length; i++){
sliderBody.appendChild(slide);
}
}
The problem is that I can't append the same element that many times. It just creates one element instead of 4.
For you to understand better I made a fiddle:
https://jsfiddle/ud7dvn3z/
Share Improve this question asked Apr 14, 2016 at 22:53 GodjeGodje 4435 silver badges16 bronze badges 1-
1
to expand on @smerny's ment: the
.cloneNode()
method of a node will return a copy of the node rather than the node itself, allowing you to use the same base node multiple times. To copy the children of the node as well, you'll need to useslide.cloneNode(true)
in order to perform a deep copy. – shamsup Commented Apr 14, 2016 at 22:58
2 Answers
Reset to default 11appendChild
will remove the node from wherever it is before appending it to its new location, so you need to make copies of the node instead. You can use cloneNode
for that. The true
makes cloneNode
perform a deep clone, i.e. with all its child nodes.
for(var i = 0; i < urls.length; i++){
sliderBody.appendChild(slide.cloneNode(true));
}
Okey guys! I found an answer. I have to put
slide = _createEl("div");
slide.className += "slide-el";
into for
loop.
Now it looks like this:
for(var i=0; i < urls.length; i++){
slide = _createEl("div");
slide.className += "slide-el";
sliderBody.appendChild(slide);
}
本文标签: javascriptHow to appendChild(element) many times (The same element)Stack Overflow
版权声明:本文标题:javascript - How to appendChild(element) many times. (The same element) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741268234a2368871.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论