我有以下html。
<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>
<p id="warranty" style="margin-top:10px"></p>
<dd>R-51B-001-5F, Return to Agilent Warranty - 5 years</dd><p>
</p>
</dl>
我在<dl class="item-options">
下的所有内容都在一个变量中假设var mycontent
。
如何删除此内容<p id="warranty" style="margin-top:10px"></p><dd>R-51B-001-5F, Return to Agilent Warranty - 5 years</dd><p></p>
来自该变量mycontent
。
mycontent
是一个将传递给弹出窗口的变量。所以我想保留
<p id="warranty" style="margin-top:10px"></p><dd>R-51B-001-5F, Return to Agilent Warranty - 5 years</dd><p></p>
在html文档中,但仅从变量中删除。
由于
答案 0 :(得分:2)
试试这个:
$(mycontent).find("#warranty").remove();
或强>
$(".item-options").find("#warranty").remove();
修改强>
一种方法是克隆() mycontent 并将其放入新变量中。您将能够保留原始 mycontent 并使用其克隆而不会影响原始元素。
var mycontentForPopup = $(mycontent).clone().find("#warranty").remove();
然后,使用 mycontentForPopup 作为弹出窗口。如果这不起作用,请给我一些反馈。
答案 1 :(得分:0)
基本上,您需要选择要删除的节点和.remove()它们。假设mycontent
是对具有类item-options
的节点的jQuery引用:
var warranty = mycontent.children('#warranty');
var txtContent = warranty.siblings('dd').last();
warranty.remove();
txtContent.remove();
如果mycontent
是DOM
节点,则只需选择jQuery
:
var $mycontent = $(mycontent);
建议构建您的DOM
,以使warranty
元素将与其相关的节点保存为children
而不是siblings
。否则语义就会丢失。
答案 2 :(得分:0)
你也可以试试这个:
$("#warranty", $(mycontent)).remove();
但我觉得代码错了:
<p id="warranty" style="margin-top:10px"></p>
<dd>R-51B-001-5F, Return to Agilent Warranty - 5 years</dd><p>
</p>
或:
<p id="warranty" style="margin-top:10px">
<dd>R-51B-001-5F, Return to Agilent Warranty - 5 years</dd>
</p>
答案 3 :(得分:0)
我从mycontent
变量中移除了保修内容
标签,如下所示。
<p id="warranty" style="margin-top:10px"></p>
<dd>R-51B-001-5F, Return to Agilent Warranty - 5 years</dd><p>
</p>
var warranty = jQuery('#warranty')。next()。html();
为myContent = mycontent.replace(保修, '');