Javascript 使用jQuery围绕另外两个div创建包装器div

Javascript 使用jQuery围绕另外两个div创建包装器div,javascript,jquery,html,Javascript,Jquery,Html,如何在2个(或更多)div周围添加div元素?我的想法是: $('div.list-price')。每个(函数(){ $(本)。在('')之前; (“”)之后的$(this.next()); }); 20000 10000您认为元素是HTML代码,但它们是对象。不能单独添加起始标记和结束标记,因为它们不是完整的元素 添加新元素,并在其中移动现有div: $('div.list-price').each(function() { var newDiv = $('<div/>')

如何在2个(或更多)div周围添加div元素?我的想法是:

$('div.list-price')。每个(函数(){
$(本)。在('')之前;
(“”)之后的$(this.next());
});

20000

10000
您认为元素是HTML代码,但它们是对象。不能单独添加起始标记和结束标记,因为它们不是完整的元素

添加新元素,并在其中移动现有div:

$('div.list-price').each(function() {
  var newDiv = $('<div/>').addClass('action-price');
  $(this).before(newDiv);
  var next = $(this).next();
  newDiv.append(this).append(next);
});
$('div.list-price')。每个(函数(){
var newDiv=$('').addClass('action-price');
美元(本)。之前(新div);
var next=$(this.next();
newDiv.append(this.append(next));
});
演示:

$first=…//第一要素
$rest=…/其他元素,可能只是第二个元素
$first.wrap(“”).parent().append($rest)

我不得不把你的最后一句话告诉:


$rest.wrap(“”).parent().append($first)

一个很好的方法是执行以下操作

$("div.list-price").each(function(){
     $(this).nextUntil("div.list-price").andSelf().wrapAll("<div class='action-price'></div>");
});
$(“div.list-price”)。每个(函数(){
$(this).nextUntil(“div.list-price”).andSelf().wrapAll(“”);
});

它简单易读,可能是jQuery开发人员想要编写它的方式。

尝试使用wrapAll函数:

$('.list-price, .sell-price').wrapAll('<div class="action-price" />');
$('.list price,.sell price').wrapAll('');

您的前后用法无效,因为这些方法不会将文本写入文档内容,而是写入节点。因此,第一个('before')插入自动关闭(bujquery本身)标记,第二个('after')由于无效的markupThx Guffa而跳过,您是对的,但每个()中缺少一行:$(this.parent('div').addClass('action-price')@riskogab:我已经添加了这个,还有一个到演示的链接。:)@布朗西:你怎么能只在前两个片子里拍?
$('.list-price, .sell-price').wrapAll('<div class="action-price" />');