How can I get a list of all class names used inside an HTML Snippet?
For example, the HTML Snippet below,
-
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)
-
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)