wrapAll()创建div的双倍?(wrapAll() is creating double the divs?)

编程入门 行业动态 更新时间:2024-10-24 04:38:05
wrapAll()创建div的双倍?(wrapAll() is creating double the divs?)

我有一个内部有两个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); });

更多推荐

本文发布于:2023-07-05 07:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1034447.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:双倍   div   wrapAll   divs   double

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!