Finding all class names used in HTML/DOM

后端 未结 8 1627
耶瑟儿~
耶瑟儿~ 2021-01-02 22:05

How can I get a list of all class names used inside an HTML Snippet?

For example, the HTML Snippet below,

相关标签:
8条回答
  • 2021-01-02 23:02

    Get all the classes & the unique ones in one dimension.

    const allClasses = Array.from(new Set(document.querySelectorAll("*").map(el => el.classNames)));
    
    const flatClasses = Array.from(new Set(allClasses.flat(1)))
    
    0 讨论(0)
  • 2021-01-02 23:03

    This is HTML:

    <div id="oye">
    <div class="pop"></div>
    <div class="hop"></div>
    <div class="bob"></div>
    </div>
    
    <p id="my"></p>
    

    In Jquery, You will get array of all class name, Check below code

     $(document).ready(function(){
      var myclass = [];
      var parent = $("#oye");
        var divs = parent.children();
    
      for(var i = 0; i < divs.length; i++){
    
        myclass.push(divs[i].className)
      }
    
      //console.log(myclass);
    
    
      var myclasses = myclass.join(', ');
    
      $("#my").html(myclasses);
    
    
    });
    

    See Demo Here: https://codepen.io/ihemant360/pen/PzWJZP

    0 讨论(0)
提交回复
热议问题