我有一个内部有两个div的容器div,就像这样:
<div class="container"> <div class="child1"></div> <div class="child2"></div> </div>我已经围绕'child1'和'child2'包裹了另一个div,但它出现了两次,我无法修复:
$(".child1, .child2").wrapAll('<div class="style"></div>');哪个呈现如下:
<div class="container"> <div class="style"> <div class="style"> <div class="child1"></div> <div class="child2"></div> </div> </div> </div>但我真正想要的是以下内容:
<div class="container"> <div class="style"> <div class="child1"></div> <div class="child2"></div> </div> </div>我该如何解决这个问题? 我尝试了许多其他尝试对双重追加进行排序的方法。
编辑:这个问题是jquery发射了两次,我把代码从现有文件中移出并放到一个新文件中。 一旦我这样做,下面的答案都有效。
I have a container div with two divs inside of it, like such:
<div class="container"> <div class="child1"></div> <div class="child2"></div> </div>I have wrapped another div around 'child1' and 'child2' but it's appearing twice which I haven't been able to fix:
$(".child1, .child2").wrapAll('<div class="style"></div>');Which is rendering out as the following:
<div class="container"> <div class="style"> <div class="style"> <div class="child1"></div> <div class="child2"></div> </div> </div> </div>But what I actually want is the following:
<div class="container"> <div class="style"> <div class="child1"></div> <div class="child2"></div> </div> </div>How do I go about fixing this? I have tried numerous other methods of trying to sort the double-append.
EDIT: The issue was jquery was firing twice, I moved the code out of the existing file and into a new file. Once I did this the answers below all worked.
最满意答案
我在小提琴中跑这个 ,它似乎工作正常...... ??????
你可以这样做......
$(".container").each(function() { var ch1 = $(this).find('.child1'); var ch2 = $(this).find('.child2'); var st = $('<div class="style">'); st.append(ch1); st.append(ch2); $(this).html('').append(st); });I ran this in fiddle and it seems to work fine... ??????
You can do this though...
$(".container").each(function() { var ch1 = $(this).find('.child1'); var ch2 = $(this).find('.child2'); var st = $('<div class="style">'); st.append(ch1); st.append(ch2); $(this).html('').append(st); });更多推荐
发布评论