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 格式的值,所以当我在弹出窗口中显示这些值时,它应该保留外观和感觉。

4

2 回答 2

2

您的代码不起作用,因为您在每次迭代中选择相同的元素。如果需要文本内容,可以使用 jQuerytext方法或textContent属性。

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

map方法返回一个数组,您可以使用 Array 对象的join方法将其转换为字符串。

于 2013-04-24T13:08:47.827 回答
1

利用children()

试试看

 $(".item-options").children(".mdata").each(function() {    
      description+=$(this).html();  
 });
于 2013-04-24T13:05:53.593 回答