繁体   English   中英

从HTML移除文字的Javascript

[英]Javascripts to remove text from HTML

我需要一个脚本来从我的产品页面中删除一行文本“库存数量:(缺货)”。 我要删除的特定HTML代码是:

<span class="PageText_L329n"><strong class="prod_qty_label">
      Quantity in Stock
    </strong></span>
      :
    <span style="color:#cc0000;"><span class="StockQuantity_OutOfStock"></span></span><br></br>

我对Javascript的了解有限,我知道我需要从以下内容开始:

document.body.innerHTML = document.body.innerHTML.replace('', '');

但是我正在努力使其与javascript中的所有HTML标记一起使用。

在我的类别页面(不同页面)上,我试图从HTML中删除此文本,该文本可能在页面中多次出现:

<b><font color="#CC0000">
<span class="PageText_L331n">(Out of Stock)</span>
</font></b>

任何帮助表示赞赏。

谢谢。

喜欢这个例子吗?

<div>
<span class="PageText_L328n"><strong class="prod_qty_label">
      Quantity in Stock 1
    </strong></span>

</div>
<div>
<span class="PageText_L329n"><strong class="prod_qty_label">
      Quantity in Stock 2
    </strong></span>

</div>
<div>
<span class="PageText_L330n"><strong class="prod_qty_label">
      Quantity in Stock 3
    </strong></span>

</div>
<button id="button">Remove</button>

var button = document.getElementById("button");
var PageText_L329n = document.getElementsByClassName("PageText_L329n");

function remove() {
    if (PageText_L329n[0]) {
        PageText_L329n[0].parentNode.removeChild(PageText_L329n[0]);
    }
}

button.addEventListener("click", remove, false);

jsfiddle上

此外,也许仅将其隐藏而不是将其从DOM中删除就足够了。

并用于清除文本

<b><font color="#CC0000">
<span class="PageText_L331n">(Out of Stock)</span>
</font></b>

<button id="clear">Clear</button>

var button = document.getElementById("clear");

function clearText() {
    document.getElementsByClassName("PageText_L331n")[0].textContent = "";
}

button.addEventListener("click", clearText, false);

jsfiddle上

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM