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

html - Remove all the DOM elements with a specific tag name in Javascript - Stack Overflow

programmeradmin2浏览0评论

How can I remove all the elements with a specific tag name using Javascript. For example, I did the following:

var els = document.getElementsByTagName("center");

and it returned an array of all the center elements. How may I remove all these elements?

Coming from Remove all child elements of a DOM node in JavaScript and JavaScript DOM remove element I know that I can loop through els, find the parent of each element and then remove that specific node. But is there anyother way provided by javascript. Like we can do $('center').remove() in jquery and it removes all the elements with center tag. Anything similar to that in Javascript?

How can I remove all the elements with a specific tag name using Javascript. For example, I did the following:

var els = document.getElementsByTagName("center");

and it returned an array of all the center elements. How may I remove all these elements?

Coming from Remove all child elements of a DOM node in JavaScript and JavaScript DOM remove element I know that I can loop through els, find the parent of each element and then remove that specific node. But is there anyother way provided by javascript. Like we can do $('center').remove() in jquery and it removes all the elements with center tag. Anything similar to that in Javascript?

Share Improve this question edited May 23, 2017 at 11:33 CommunityBot 11 silver badge asked Nov 18, 2013 at 9:29 Kamran AhmedKamran Ahmed 12.4k23 gold badges72 silver badges103 bronze badges 4
  • 1 stackoverflow.com/questions/3387427/… – Mordalthunder Commented Nov 18, 2013 at 9:30
  • Yes there is but you still have to loop. – Bergi Commented Nov 18, 2013 at 9:31
  • FWIW, the jQuery $('center').remove() will still loop though the nodes internally. – Matt Commented Nov 18, 2013 at 9:36
  • using pure javascript,if you need to remove certain tags,you will have to traverse the DOM. Traversing is not possible without looping. – HIRA THAKUR Commented Nov 18, 2013 at 9:43
Add a comment  | 

2 Answers 2

Reset to default 15

With the mention that you still loop over the elements (there is no way to avoid that), you can do this:

Array.prototype.slice.call(document.getElementsByTagName('center')).forEach(
  function(item) {
    item.remove();
    // or item.parentNode.removeChild(item); for older browsers (Edge-)
});

DEMO: http://jsbin.com/OtOyUVE/1/edit

Some notes on slice:

document.getElementsByTagName doesn't return an array, it returns a live list with a length property. That is why it is needed to first convert it into an array (Array.prototype.slice does that for any object with the length property). By doing that you eliminate the problem of the list being live (gets updated when you change the DOM) and you also get the other array functions to work (like forEach) which have a more functional syntax for looping.

"it returned an array of all the center elements."

Well, it returned an array-like object (a live NodeList or an HTMLCollection depending on the browser).

"How may I remove all these elements?"

You have to loop through the list removing them one at a time as you mentioned later in your question. If you find yourself doing that a lot, encapsulate the looping inside a function so that you don't have to repeat it.

"we can do $('center').remove() in jquery and it removes all the elements with center tag. Anything similar to that in Javascript?"

jQuery is a collection of JavaScript functions, so it can't do anything JavaScript can't do. jQuery's .remove() method (like most other jQuery methods) will loop internally, it just saves you having to think about it. So that comes back to what I already mentioned above, encapsulate the loop/remove code in a function so that you can use it from any part of your code.

发布评论

评论列表(0)

  1. 暂无评论