简体   繁体   中英

Javascripts to remove text from HTML

I need a script to remove a line of text "Quantity in Stock:(Out of Stock)" from my product pages. The specific HTML code I'm trying to remove is:

<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>

I only have limited knowledge of Javascript, and I know I need to start with something like this:

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

But I'm struggling to make it work with having all the HTML markups in the javascript.

On my category pages (different page), I am trying to remove this text from the HTML, which may appear multiple times in the page:

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

Any help is appreciated.

Thanks.

Like this example?

<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);

on jsfiddle

Also, maybe it is enough just to hide it rather than remove it from the DOM.

And for clearing text

<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);

on jsfiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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