1

我有以下 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>

请帮忙,我开始失去理智了。

4

1 回答 1

4

这应该这样做

​$("img")​.each(function()
{
    $(this).prevUntil("img").wrapAll("<div class='WRAP_CONTAINER'></div>");
})​;​
于 2012-11-16T19:40:20.083 回答