Recursive thinking to obtain all tag elements on the page (remove duplication)

不言
Release: 2023-03-22 18:04:01
Original
1518 people have browsed it

This article shares with you the code that uses recursive thinking to obtain all tag elements on the page. Friends in need can refer to this code

var tag = [];
var search = function($element, tag){
  var localName = $element[0].localName;
  if(!tag.includes(localName)){
    tag.push($element[0].localName);
  }
  var children = $element.children();
  if(children.length > 0) {
    children.each(function(e){
      search($(this), tag);
    });
  }
  return tag;
};

tag = search($('html'), tag);
console.log(tag);
Copy after login

Use recursive thinking to traverse all elements and finally return an array
Recursive thinking to obtain all tag elements on the page (remove duplication)

Related recommendations:

Two methods to create multi-level directories in PHP


The above is the detailed content of Recursive thinking to obtain all tag elements on the page (remove duplication). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!