如何使用Javascript删除文本

时间:2018-06-13 13:33:53

标签: javascript

我想用JavaScript删除最后两个span标记。我的代码是

<h1 class='av-special-heading-tag'  itemprop="headline"  >Turning </h1><div class='special-heading-border'><div class='special-heading-inner-border' ></div></div></div>
<span class="av_font_icon avia_animate_when_visible av-icon-style-  av-no-color avia-icon-pos-center " style=""><span class='av-icon-char' style='font-size:20px;line-height:20px;' aria-hidden='true' data-av_icon='' data-av_iconfont='entypo-fontello' ></span></span>
<section class="av_textblock_section"  itemscope="itemscope" itemtype="https://schema.org/CreativeWork" ><div class='avia_textblock '  style='font-size:20px; '  itemprop="text" ><p style="text-align: center;">
<span style="color: #641380;">Mazak Nexus 200MY</span><br />
<span style="color: #641380;"> Mazak Nexus QNT 200MY</span><br />
<span style="color: #641380;"> Mazak Nexus QNT 200MY</span><br />
<span style="color: #641380;"> Mazak SQT200M</span><br />
<span style="color: #641380;"> Mazak SQT10M</span><br />
<span style="color: #641380;"> Mazak Nexus</span><br />
<span style="color: #641380;"> Mazak QT10</span><br />
<span style="color: #641380;"> Mazak QT10</span><br />
<span style="color: #641380;"> Mazak QT8</span><br />
<span style="color: #641380;"> Mazak Colchester lathe</span><br />
<span style="color: #641380;"> Mazak Harrison lathe</span></p>
</div></section></div> 

所以想要删除

<span style="color: #641380;"> Mazak Colchester lathe</span><br />
<span style="color: #641380;"> Mazak Harrison lathe</span></p> 

使用Javascript。

谢谢。

我试过这个但没有工作 -

 <script type="text/javascript">
 string.removeWord('colchester'); 
 </script>

2 个答案:

答案 0 :(得分:1)

if (x < 2)条件是您要删除的记录数。

&#13;
&#13;
var spans = document.querySelectorAll(".avia_textblock p span");

var x = 0;
for (var i = spans.length - 1; i >= 0; i--) {
  if (x < 2) {
    spans[i].parentNode.removeChild(spans[i]);
    x++;
  } else {
    break;
  }
}
&#13;
<h1 class='av-special-heading-tag' itemprop="headline">Turning </h1>
<div class='special-heading-border'>
  <div class='special-heading-inner-border'></div>
</div>
<span class="av_font_icon avia_animate_when_visible av-icon-style-  av-no-color avia-icon-pos-center " style=""><span class='av-icon-char' style='font-size:20px;line-height:20px;' aria-hidden='true' data-av_icon='' data-av_iconfont='entypo-fontello' ></span></span>
<section class="av_textblock_section" itemscope="itemscope" itemtype="https://schema.org/CreativeWork">
  <div class='avia_textblock ' style='font-size:20px; ' itemprop="text">
    <p style="text-align: center;">
      <span style="color: #641380;">Mazak Nexus 200MY</span><br />
      <span style="color: #641380;"> Mazak Nexus QNT 200MY</span><br />
      <span style="color: #641380;"> Mazak Nexus QNT 200MY</span><br />
      <span style="color: #641380;"> Mazak SQT200M</span><br />
      <span style="color: #641380;"> Mazak SQT10M</span><br />
      <span style="color: #641380;"> Mazak Nexus</span><br />
      <span style="color: #641380;"> Mazak QT10</span><br />
      <span style="color: #641380;"> Mazak QT10</span><br />
      <span style="color: #641380;"> Mazak QT8</span><br />
      <span style="color: #641380;"> Mazak Colchester lathe</span><br />
      <span style="color: #641380;"> Mazak Harrison lathe</span></p>
  </div>
</section>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

试试这个

ROOT