javascript - jQuery 添加 div 的第一部分,然后分别添加 div 的最后一部分

标签 javascript jquery html add wrapall

我正在尝试将以下价格和文本包装在一起,全部放在一个 div 中。

这是我的:

<table cellspacing="0" cellpadding="0" border="0" class="thePrices">
  <tbody>
    <tr>
      <td>
        <font class="text colors_text">
          <b>MSRP: <span class="priceis">$90.00</span></b>
        </font><br>
        <b><font class="pricecolor colors_productprice">
                     <font class="text colors_text"><b>Burkett Price:</b></font>
        <span class="priceis">$289<sup>.99</sup></span>
        </font>
        </b><br>
        <a href="a link">A link goes here</a>
        <table>A TABLE WITH STUFF GOES HERE</table>
      </td>
    </tr>
  </tbody>
</table>

这就是我想要得到的:

<table cellspacing="0" cellpadding="0" border="0" class="thePrices">
  <tbody>
    <tr>
      <td>
        **
        <div class="pricebox">**
          <font class="text colors_text">
            <b>MSRP: <span class="priceis">$90.00</span></b>
          </font><br>
          <b><font class="pricecolor colors_productprice">
                         <font class="text colors_text"><b>Burkett Price:</b></font>
          <span class="priceis">$289<sup>.99</sup></span>
          </font>
          </b><br> **
        </div>**
        <a href="a link">A link goes here</a>
        <table>A TABLE WITH STUFF GOES HERE</table>
      </td>
    </tr>
  </tbody>
</table>

这行不通,但您知道我做错了什么:

$(document).ready(function () {
$('.thePrices').find('td').find('font:first').before('<div class="pricebox">');
$('.thePrices').find('.colors_productprice').find('b').after('</div>');
});

也许我应该使用 SLICE?

最佳答案

我想你想要的是 .wrapAll()
已更新,使用 .andSelf()包括 <font/>

$('.thePrices').find('td').children().first().nextUntil('a').andSelf().wrapAll('<div class="pricebox"></div>');

Example on jsfiddle

关于javascript - jQuery 添加 div 的第一部分,然后分别添加 div 的最后一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5956034/

相关文章:

javascript - 如何连接两个 getElementsBy

javascript - 如何更改 mouseup 上的光标?

javascript - Select2 相关下拉列表

javascript - 如何在字体大小更改时停止输入调整大小

javascript - Knockout.js 中的事件绑定(bind)

javascript - 选择类时 jQuery 选择器速度较慢

html - 尺寸不适用于 HTML5 输入标签

javascript - 为什么canvas函数getImageData在不同情况下执行时耗时差异很大

html - 从用户体验的角度来看,什么反对单页应用程序?

html - HTML5 <音频>在Firefox中停滞