我想要选择所有的元素,除了第一个元素在一个div与该类。
当前HTML:
<div class="cpt-cv-content-item">
<a>...</a>
<h4>...</h4>
...
</div>
<div class="cpt-cv-content-item">
<a>...</a>
<h4>...</h4>
...
</div>我试过使用wrap:
$(".pt-cv-content-item :not(:first-child)").wrapAll( "<div class='new'></div>" );这个函数正确地选择了除第一个以外的所有元素,但将它们全部移动到第一个div中。
我想变成这样:
<div class="cpt-cv-content-item">
<a>...</a>
<div class='new'>
<h4>...</h4>
...
</div>
</div>
<div class="cpt-cv-content-item">
<a>...</a>
<div class='new'>
<h4>...</h4>
...
</div>
</div>不幸的是,我无法编辑html。提前谢谢你
发布于 2015-09-27 15:06:40
包装整个内容,然后将第一个子.new移出主div。
使用wrapInner()包装所有的子级。使用prependTo()移动元素启动元素。
$(".cpt-cv-content-item").wrapInner($("<div class='new'>"));
$('div.new').each(function(){
$(this).children().first().prependTo($(this).closest(".cpt-cv-content-item"));
})<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="cpt-cv-content-item">
<a>...</a>
<h4>...</h4>
...
</div>
<div class="cpt-cv-content-item">
<a>...</a>
<h4>...</h4>
...
</div>
发布于 2015-09-27 14:59:25
使用wrap而不是wrapAll。
$(".pt-cv-content-item :not(:first-child)").wrap( "<div class='new'></div>" );包装
描述:在匹配的元素集合中围绕每个元素包装一个HTML结构。
包皮
描述:围绕匹配元素集中的所有元素包装HTML结构。
发布于 2015-09-27 17:53:42
我选择了‘..pt cv-content-item’中的所有元素:
$(".pt-cv-content-item ").wrapInner( "<div class='new'></div>" );我搬出了‘..pt cv-content-item’div中的第一个href
$(".new a:first-child").each(function() {
$(this).parent().before(this);
});谢谢你们
https://stackoverflow.com/questions/32809242
复制相似问题