最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Adding parent div over multiple divs - Stack Overflow

programmeradmin1浏览0评论

I want to add a parent div.

<div id="faceone"><img src=""></div>

<div id="facetwo" ><img src=""></div>

<div id="facethree"><img src=""></div

<div id="facefour"><img src=""></div>

These are four divs and i want to add a parent div above them. I tried this

$('#faceone').wrapAll('<div class="cubeSpinner">'); 

But this is adding cubeSpinner above faceone only

<div class="cubeSpinner">

<div id="faceone"><img src=""></div>

</div>
<div id="facetwo" ><img src=""></div>

<div id="facethree"><img src=""></div

<div id="facefour"><img src=""></div>

But i want the result should be like this

<div class="cubeSpinner">

<div id="faceone"><img src=""></div>

<div id="facetwo" ><img src=""></div>

<div id="facethree"><img src=""></div

<div id="facefour"><img src=""></div>

</div>

I want to add a parent div.

<div id="faceone"><img src=""></div>

<div id="facetwo" ><img src=""></div>

<div id="facethree"><img src=""></div

<div id="facefour"><img src=""></div>

These are four divs and i want to add a parent div above them. I tried this

$('#faceone').wrapAll('<div class="cubeSpinner">'); 

But this is adding cubeSpinner above faceone only

<div class="cubeSpinner">

<div id="faceone"><img src=""></div>

</div>
<div id="facetwo" ><img src=""></div>

<div id="facethree"><img src=""></div

<div id="facefour"><img src=""></div>

But i want the result should be like this

<div class="cubeSpinner">

<div id="faceone"><img src=""></div>

<div id="facetwo" ><img src=""></div>

<div id="facethree"><img src=""></div

<div id="facefour"><img src=""></div>

</div>
Share Improve this question asked Nov 26, 2015 at 0:43 Owais AhmedOwais Ahmed 1,4382 gold badges35 silver badges72 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 10

You need to select all the elements in order for them to be wrapped. I'd suggest adding a mon class to them, but in the meantime you could use the attribute selector [id^="face"] to select all elements with an id attribute that begins with 'face':

$('[id^="face"]').wrapAll('<div class="cubeSpinner"></div>'); 

I've shown this example on JSfiddle https://jsfiddle/azu2s4r9/

You should use the .wrapAll() jquery function for this and add a mon class name to each of the four existing <div> tags. Your new original code will look like this.

<div id="faceone" class="test"><img src=""></div>
<div id="facetwo" class="test"><img src=""></div>
<div id="facethree" class="test"><img src=""></div>
<div id="facefour" class="test"><img src=""></div>

Then your jquery code should look like this.

$('.test').wrapAll('<div class="cubeSpinner">Testing</div>');

The resulting code will look like this which is how you wanted it to finish.

<div class="cubeSpinner">Testing
<div id="faceone" class="test"><img src=""></div>
<div id="facetwo" class="test"><img src=""></div>
<div id="facethree" class="test"><img src=""><div>
<div id="facefour" class="test"><img src=""></div>
</div>

This provides a plete solution and should work in pretty much all cases. Good luck!

发布评论

评论列表(0)

  1. 暂无评论