2013-04-24 102 views
1

我有以下HTML。使用jQuery,我想在一個變量中獲取<dl class="item-options">下每個HTML元素的值。如何找到一個父元素下的每個元素的值?

<dl class="item-options">被表中的多個時間具有不同值的每一次產生的下<dl class="item-options">

<dl class="item-options"> 
     <dd class="truncated" style="color:red;">DSOX3000-232, RS232/UART Serial Decode and Trigger - in <a onclick="return false" class="dots" href="#">...</a>/<div class="truncated_full_value"><dl class="item-options"><dt>Software Applications</dt><dd id="pppp">DSOX3000-232, RS232/UART Serial Decode and Trigger - installed, DSOX3000-AMS, CAN and LIN Automotive Serial Decode - installed, DSOX3000-MAT, Advanced Math Analysis for Infiniivision Oscilloscopes - installed, DSOX3000-VID, Enhanced Video/TV Application Package - installed/</dd></dl></div> 
     </dd>   

     <div class="mdata"> 
      <dd class="truncated">DSOX3000-001, WaveGen 20 MHz Function/Arbitrary Wavefor <a onclick="return false" class="dots" href="#">...</a>/<div class="truncated_full_value"><dl class="item-options"><dt>Advanced Analysis</dt><dd id="pppp">DSOX3000-001, WaveGen 20 MHz Function/Arbitrary Waveform Generator - installed/</dd></dl></div></dd> 
     </div>   

     <div class="mdata"> 
      <dd>DSOX3000-040, Memory Upgrade - 4 Mpts of MegaZoom IV/</dd> 
     </div>   

     <div class="mdata"> 
      <dd class="truncated">DSOX3000-805, Module - LAN/VGA, DSOX3000-806, Module - <a onclick="return false" class="dots" href="#">...</a>/<div class="truncated_full_value"><dl class="item-options"><dt>Connectivity</dt><dd id="pppp">DSOX3000-805, Module - LAN/VGA, DSOX3000-806, Module - GPIB/</dd></dl></div></dd> 
     </div>   

     <div class="mdata"> 
      <dd class="truncated">DSO0000-903, Power cord - United States and Canada 120V <a onclick="return false" class="dots" href="#">...</a>/<div class="truncated_full_value"><dl class="item-options"><dt>Power Cords</dt><dd id="pppp">DSO0000-903, Power cord - United States and Canada 120V, NEMA 5-15P male plug/</dd></dl></div></dd> 
     </div>   

     <div class="mdata"> 
      <dd class="truncated">DSOX3000-A6J, Certificate of compliance calibration - A <a onclick="return false" class="dots" href="#">...</a>/<div class="truncated_full_value"><dl class="item-options"><dt>Calibration - Upgrade Commercial Calibration Certificate</dt><dd id="pppp">DSOX3000-A6J, Certificate of compliance calibration - ANSI Z540, printed/</dd></dl></div></dd> 
     </div>   

     <p style="margin-top:37px"></p> 
      <dd>R-50C-021-5, ANSI Z540-1-1994 Calibration - 5 years</dd> 
     <p></p>   

     <p style="margin-top:10px"></p> 
      <dd>R-51B-001-5F, Return to Agilent Warranty - 5 years</dd><p> 
     </p>    
</dl> 

我使用以下代碼來選擇所有元素的值,其是<dl class="item-options">下:

$(".item-options").each(function() {  
       description+=$(".mdata").find("*").html(); 
}); 

但我無法獲取所有值。什麼是正確的方法來做到這一點?

我想要html格式的值,所以當我在彈出窗口中顯示這些值時,它會保留外觀和感覺。

+0

你可以發表你期待的輸出的例子嗎? – Terence 2013-04-24 14:13:41

+0

@Terence實際上當我將鼠標懸停在

上時,我希望所有的子元素都顯示在彈出窗口中。 – Muk 2013-04-24 14:18:46

回答

2

因爲你是選擇在每次迭代中相同的元素,您的代碼不起作用。如果您需要文本內容,則可以使用jQuery text方法或textContent屬性。

var text = $(".item-options").children().map(function() { 
     return $(this).text(); 
}).get().join(''); 

map方法返回一個數組,你可以將其轉換爲使用Array對象的join方法的字符串。

+0

我想要

下的每個html元素的值> – Muk 2013-04-24 13:15:14

+0

@Muk請參閱http://jsfiddle.net/SuEp5/ – undefined 2013-04-24 13:16:46

1

使用children()

試部份

$(".item-options").children(".mdata").each(function() {  
     description+=$(this).html(); 
}); 
相關問題