最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I remove a footer element using Javascript - Stack Overflow

programmeradmin3浏览0评论

I have HTML:

<footer class="footer">
    <div class="container-fluid wrapper">
    ...
    </div>
</footer>

How do I remove the whole footer markup using javascript (no jQuery available)?

I've tried:

var elem = document.getElementsByName("footer");
elem.remove();

...and a couple of other variations, but I can't get it to delete.

Any ideas?

Thanks, Mark

I have HTML:

<footer class="footer">
    <div class="container-fluid wrapper">
    ...
    </div>
</footer>

How do I remove the whole footer markup using javascript (no jQuery available)?

I've tried:

var elem = document.getElementsByName("footer");
elem.remove();

...and a couple of other variations, but I can't get it to delete.

Any ideas?

Thanks, Mark

Share Improve this question asked Mar 28, 2017 at 14:43 Mark TaitMark Tait 6454 gold badges16 silver badges29 bronze badges 3
  • Why do you want to remove it? Would hiding it with a css modifier not be acceptable? Also you need to identify which element in the returned collection you want to actually remove – SierraOscar Commented Mar 28, 2017 at 14:44
  • Use removeChild via the parent, E.g. stackoverflow./questions/3387427/remove-element-by-id – Alex K. Commented Mar 28, 2017 at 14:44
  • Open your developer console, and you'll see the error. You can't call .remove() on a collection of elements. – user1106925 Commented Mar 28, 2017 at 14:47
Add a ment  | 

4 Answers 4

Reset to default 2

Yes, you can do like this

function removeTagByTagName(tagName) {
  var ele = document.getElementsByTagName(tagName);
  return ele[0].parentNode.removeChild(ele[0]);
}

function removeTag(tag) {
  var ele = document.getElementsByTagName(tag);
  return ele[0].parentNode.removeChild(ele[0]);
}

var btn = document.getElementById("delet");
btn.addEventListener("click", function(){
  removeTagByTagName("footer");
});
<body>
<button id="delet">Delete Footer!</button>

  <footer class="footer" name="footer">
    <div class="container-fluid wrapper">
      blab bal babla
    </div>
  </footer>
</body>

You would need to grab that specific footer element. What you have with var elem = document.getElementsByName("footer"); grabs a collection of all elements named "footer" but if you want to do it that way, you need to add the name="footer" attribute to your footer element. The way your HTML is set up right now, you could change that line to:

var elem = document.getElementsByTagName("footer");

If you only have one footer element, then you can target that one like this:

var elem = document.getElementsByTagName("footer")[0];

Otherwise, you could assign that element an ID, or figure out which footer item in the collection it was (i.e. document.getElementsByTagName("footer")[3]).

Once you have that specific element, you can remove it like this:

elem.parentNode.removeChild(elem);

you cannot use .remove with all browsers, since the support is not that good yet. I would remend polyfilling the remove, so that you can use this. Use the following polyfill (taken from MDN):

// from:https://github./jserz/js_piece/blob/master/DOM/ChildNode/remove()/remove().md
(function (arr) {
  arr.forEach(function (item) {
    if (item.hasOwnProperty('remove')) {
      return;
    }
    Object.defineProperty(item, 'remove', {
      configurable: true,
      enumerable: true,
      writable: true,
      value: function remove() {
        this.parentNode.removeChild(this);
      }
    });
  });
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);

Now you can use .remove() with ease.

You can also use .removeChild() if you know the parent of the node you want to delete. Something like this:

var parent = document.getElementById("div1");
var child = document.getElementById("p1");
parent.removeChild(child);

So since your is inside the , you can treat the body as the parent and remove its child () using similar code as above snippet.

The removeChild function and querySelector can be used to fulfil your needs.

function remove(){
  var el=document.querySelector('footer.footer');
  el.parentNode.removeChild(el);
}
remove();
<footer class="footer">
    <div class="container-fluid wrapper">
    ...
    </div>
</footer>

发布评论

评论列表(0)

  1. 暂无评论