简体   繁体   中英

How to delete specific div element with Js?

I want to delete specific div element, when it is loaded another page than homepage. I wrote such js code:

<script>
    window.onload = function() {
        if (window.location.href != 'http://stackoverflow.com/')
            document.getElementById('panel').remove();

    }
</script>

I put it in code and put in WIdget.

Html code:

<div id="panel">
bla bla bla 
</div>

It doesn't work anymore I try also this:

<script>
    window.onload = function() {
          var loc = http://stackoverflow.com/;
        if (window.location.href != loc)
            document.getElementById('panel').remove();

    }
</script>

I know that remove sometimes doesn't work, so try this:

Element.prototype.remove = function() {
    this.parentElement.removeChild(this);
}
NodeList.prototype.remove = HTMLCollection.prototype.remove = function() {
    for(var i = this.length - 1; i >= 0; i--) {
        if(this[i] && this[i].parentElement) {
            this[i].parentElement.removeChild(this[i]);
        }
    }
}

And then you can remove elements like this

document.getElementById("my-element").remove();

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