简体   繁体   English

将div附加到新的div

[英]Append a div to a new div

i am using IPB and i am going to put each category into a new tab the category div is something like this: 我正在使用IPB,并且将每个类别放到新标签中,类别div如下所示:

<div id='category_100'>
<div id='category_104'>
<div id='category_102'>
<div id='category_101'>

and my tabs content is like this: 我的标签页内容如下:

<div class="content">
                    <div id="content-1" class="content-1">

                    </div>
</div>

and the categories divs is already showing but i want it to be moved to the content-1 div without duplicate so i want it to move from its div to this div with jjava script how? 并且类别div已经显示,但是我希望将其移至content-1 div而无需重复,所以我希望它通过jjava脚本从其div移至该div?

<script>
  document.getElementById('content-1').appendChild(
    document.getElementById('category_100')
  );
</script>

this worked for me but how can i add more than id to category_100 这为我工作,但我如何添加超过id到category_100

i want it to be like this in one script code so i would not repeart the scrip code four times: 我希望它在一个脚本代码中是这样的,所以我不会四次重复脚本代码:

<div class="content">
                    <div id="content-1" class="content-1">
                        <div id='category_100'>
<div id='category_104'>
<div id='category_102'>
<div id='category_101'> 
                    </div>
</div>

using my two lines the suggested things here is not working! 用我的两行话,这里建议的事情是行不通的!

Try this code : 试试这个代码:

$('div[id^="category_"]').appendTo('#content-1')

Have a look to this fiddle : http://jsfiddle.net/lulu3030/sjEPx/2/ 看看这个小提琴: http : //jsfiddle.net/lulu3030/sjEPx/2/

"using my two lines the suggested things here is not working!" “用我的两行话,这里建议的东西行不通!”

You just get a single element with an ID and trying to append it... 您只获得一个具有ID的元素并尝试附加它...

Live Demo 现场演示

If you want to append multiple elements, there are many ways... 如果要附加多个元素,可以采用多种方法...

Wrap those elements and then append... 包装这些元素,然后附加...

<div id="categories">
    <div id='category_100'></div>
    <div id='category_104'></div>
    <!-- etc. -->
</div>

document.getElementById('content-1').appendChild(document.getElementById('categories'));

or add same class to all elements that you want to append... 或向要添加的所有元素添加相同的类...

<div id='category_100' class="myClass"></div>
<div id='category_104' class="myClass"></div>

[].forEach.call(document.getElementsByClassName("myClass"), function (value, index, array) {
    document.getElementById("content-1").appendChild(value);
});

or get elements with query selector that match some pattern... 或使用匹配某些模式的查询选择器获取元素...

[].forEach.call(document.querySelectorAll("div[id^='category_']"), function (value, index, array) {
    document.getElementById("content-1").appendChild(value);
});

and etc. 等等。

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

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