简体   繁体   English

使用javascript将第一个元素之后的所有元素添加到div中(无jQuery)

[英]Add all elements after first element to a div by using javascript (No jQuery)

I have the following html 我有以下HTML

<div id="main">
<aside id="list"><p>sometext</p></aside>
<aside id="list-2"><p>sometext</p></aside>
<aside id="list-3"><p>sometext</p></aside>
<aside id="list-4"><p>sometext</p></aside>
</div>

I want to use javascript to make it look like : 我想使用javascript使其看起来像:

<div id="main">
<aside id="list"><p>sometext</p></aside>
<div id="wrap">
<aside id="list-2"><p>sometext</p></aside>
<aside id="list-3"><p>sometext</p></aside>
<aside id="list-4"><p>sometext</p></aside>
</div>
</div>

I have tried insertAdjacentHTML and innerHTML methods : 我尝试了insertAdjacentHTMLinnerHTML方法:

widgets = document.getElementById('main');
widgets.innerHTML = "<div class='box-class'>" + widgets.innerHTML + "</div>";

But this adds wrapper over "list" too. 但这也将包装器添加到“列表”上。

There are two big problems with the code you said you tried (three if widgets isn't declared anywhere): 您说过尝试的代码有两个大问题(如果未在任何地方声明widgets则有三个问题):

 widgets = document.getElementById('main'); widgets.innerHTML = "<div class='box-class'>" + widgets.innerHTML + "</div>"; 
  1. Using strings means the browser has to go through the elements, build an HTML string for them, and return that string to JavaScript; 使用字符串意味着浏览器必须浏览这些元素,为其构建HTML字符串,然后将该字符串返回给JavaScript; then when you assign to innerHTML it has to destroy the elements that are already there and build new replacement ones by parsing the HTML string. 然后,当您分配给innerHTML它必须销毁已经存在的元素,并通过解析HTML字符串来构建新的替换元素。 This will wipe out any event handlers or similar attached to the elements. 这将清除所有附加到元素的事件处理程序或类似事件。 (Of course, if there aren't any, it doesn't matter much.) (当然,如果没有,那没关系。)
  2. That wraps all of the children, not just the ones after the first child. 这包裹了所有孩子,而不仅仅是第一个孩子之后的孩子。

(It also wraps them in <div class='box-class'> , not <div id="wrap"> , but...) (还将它们包装在<div class='box-class'> ,而不是<div id="wrap"> ,但是...)

On all modern browsers, elements have a children list you can use for this. 在所有现代浏览器上,元素都有一个可以用于此目的的children列表。 Then just create a wrapper, move the children other than the first into it, and append it. 然后,只需创建一个包装器,将第一个包装器以外的其他包装器移入包装器,然后将其附加。

var main = document.getElementById("main");
var wrapper = document.createElement("div");
wrapper.id = "wrap";
while (main.children.length > 1) {
    // Note: Appending the element to a new parent removes it from its original
    // parent, so `main.children.length` will decrease by 1
    wrapper.appendChild(main.children[1]);
}
main.appendChild(wrapper);

Example: 例:

 var main = document.getElementById("main"); var wrapper = document.createElement("div"); wrapper.id = "wrap"; while (main.children.length > 1) { wrapper.appendChild(main.children[1]); } main.appendChild(wrapper); 
 #wrap { border: 1px solid blue; } 
 <div id="main"> <aside id="list"> <p>sometext</p> </aside> <aside id="list-2"> <p>sometext</p> </aside> <aside id="list-3"> <p>sometext</p> </aside> <aside id="list-4"> <p>sometext</p> </aside> </div> 


Side note: In your markup, you have </div> where you want </aside> . 旁注:在标记中,您有</div>位置</aside>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM