从页面中删除没有标记的文本

时间:2014-12-13 16:46:45

标签: javascript jquery css

我有正确显示的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 

2 个答案:

答案 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 内容,而不是它内的跨度,你可以这样做:

&#13;
&#13;
$(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;
&#13;
&#13;