我试图将所有来自特定元素的Div包裹起来。我怎么能用jQuery做到这一点?
当前代码
<div class="a"></div>
<div class="b"></div>
<div class="b"></div>
<div class="a"></div>
<div class="b"></div>
<div class="b"></div>
<div class="a"></div>
<div class="b"></div>
<div class="b"></div>
所需输出
<div class="a"></div>
<div class="wrapper">
<div class="b"></div>
<div class="b"></div>
</div>
<div class="a"></div>
<div class="wrapper">
<div class="b"></div>
<div class="b"></div>
</div>
<div class="a"></div>
<div class="wrapper">
<div class="b"></div>
<div class="b"></div>
</div>
答案 0 :(得分:1)
您可以为每个SELECT
pd.name AS 'Product Name',
p.model AS UPC,
p.quantity AS 'Quantity',
p.price AS 'Regular Price',
ps.price AS 'Special Price',
p.cost AS 'COST',
p.status AS 'Status'
FROM oc_product p
INNER JOIN oc_product_description pd
ON pd.product_id = p.product_id
INNER JOIN oc_product_special ps
ON ps.product_id = p.product_id
INNER JOIN oc_manufacturer m
ON p.manufacturer_id = m.manufacturer_id
INNER JOIN oc_product_to_category p2c
ON p2c.product_id = p.product_id
INNER JOIN oc_category c
ON c.category_id = p2c.category_id
INNER JOIN oc_category_description cd
ON c.category_id = cd.category_id
WHERE
c.category_id = 37 OR c.parent_id = 37
GROUP BY pd.name ORDER BY m.name ASC
创建wrap元素,将所有元素添加到下一个.a
元素,然后添加到DOM。
.a
$('.a').each(function() {
var wrap = $('<div class="wrapper"></div>');
$(this).nextUntil('.a').appendTo(wrap);
$(this).after(wrap);
})
.wrapper {
border: 1px solid black;
}
答案 1 :(得分:1)
如果总有2个.b
,您可以使用以下代码:
$('.a').each(function() {
$(this).next('.b').next('.b').andSelf().wrapAll('<div class="wrapper"/>');
});
.wrapper {
border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="a">A</div>
<div class="b">B</div>
<div class="b">B</div>
<div class="a">A</div>
<div class="b">B</div>
<div class="b">B</div>
<div class="a">A</div>
<div class="b">B</div>
<div class="b">B</div>