我有正确显示的html页面,但它有一些动态文本即将到来,并且它没有标记。如何使用jquery删除该文本。
案文即将发布:
<div id="Container2" class="tab default-tab" style="display: block; margin-top: -15px; overflow: auto; width: 990px; height: 400px;">
<!-- start -->
Search Setup 1 FIA - need to remove this {the text appearing is dynamic}
<style>other contents go here
答案 0 :(得分:0)
此代码在div中查找原始文本并将其删除
$("#Container2").contents().each(function(index, elem) {
if ($.trim(elem.nodeValue) != '' && elem.nodeType == 3){
alert($.trim(elem.nodeValue))
$(elem).remove();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="Container2" class="tab default-tab" style="display: block; margin-top: -15px; overflow: auto; width: 990px; height: 400px;">
<!-- start -->
Search Setup 1 FIA - need to remove this {the text appearing is dynamic}
<span>other contents go here</span>
</div>
答案 1 :(得分:0)
假设你的html是更多的
<div id="Container2" class="tab default-tab">
<!-- start -->
Search Setup 1 FIA - need to remove this {the text appearing is dynamic}
<span>other contant here</span>
</div>
你只想摆脱div的 text 内容,而不是它内的跨度,你可以这样做:
$(function(){
$('#Container2').contents()
.filter(function(){
return this.nodeType == 3; // text nodes
})
.remove();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="Container2" class="tab default-tab">
<!-- start -->
Search Setup 1 FIA - need to remove this {the text appearing is dynamic}
<span>other contant here</span>
</div>
&#13;