我有以下HTML:
<div class="main_container">
<a href="#">Financial</a>
<div class="sub_container">
<a href="#">Financial services</a>
<a href="#">Government</a>
</div>
<img class="image" src="img/img.png">
<a href="#">Education</a>
<div class="sub_container">
<a href="#">Broadcast and media</a>
<a href="#">Education</a>
</div>
<a href="#">Government</a>
<div class="sub_container">
<a href="#">Broadcast and media</a>
<a href="#">Government</a>
</div>
<img class="image" src="img/img.png">
</div>
我试图绕过img标签上面的所有元素,没有结果,我已经尝试使用wrap,wrapAll,prevAll但是那些不起作用。
输出代码我试图获得:
<div class="main_container">
<div class="WRAP_CONTAINER">
<a href="#">Financial</a>
<div class="sub_container">
<a href="#">Financial services</a>
<a href="#">Government</a>
</div>
</div>
<img class="image" src="img/img.png">
<div class="WRAP_CONTAINER">
<a href="#">Education</a>
<div class="sub_container">
<a href="#">Broadcast and media</a>
<a href="#">Education</a>
</div>
<a href="#">Government</a>
<div class="sub_container">
<a href="#">Broadcast and media</a>
<a href="#">Government</a>
</div>
</div>
<img class="image" src="img/img.png">
</div>
请帮助,我开始失去理智。
答案 0 :(得分:4)
这应该做到
$("img").each(function()
{
$(this).prevUntil("img").wrapAll("<div class='WRAP_CONTAINER'></div>");
});