2011-05-10 61 views
5

我試圖將以下價格和文本放在一起,都放在一個DIV中。jQuery添加div的第一部分,然後添加div的最後一部分seperatley

這是我有:

<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?

+0

你能解釋更多關於你想要輸出是什麼樣子? – Dhiraj 2011-05-10 20:31:32

+0

當然,我只想把第一個字體標籤和B標籤一起包裝在一個div中。在本例中,類爲'pricebox'。 – ToddN 2011-05-10 20:34:16

回答

5

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

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

Example on jsfiddle

+0

不錯的提示,我不知道'nextUntil',所以我不認爲'wrap'可以用於這個。 – 2011-05-10 20:34:47

+0

現在這只是包裝產品價格部分,而不是包裝中的MSRP部分。 – ToddN 2011-05-10 20:35:40

+0

@Mark這對我無法正常工作。我需要它也包裝第一個FONT標籤,它只是在TD下包裝第一個B標籤? – ToddN 2011-05-10 20:46:45

0
$('.thePrices>tbody>tr>td').children().slice(0,3).wrapAll('<div class="pricebox"></div>'); 

用途slice()包裝之前,片中的第一3個孩子他們注意到一個<br>是一個孩子,如果你想包裝bra之前更改3到4

相關問題