简体   繁体   English

如何使用jQuery获取所有div标签类属性

[英]how to get all div tags class attributes using jquery

I want to display all class name attributes which is inside a div. 我想显示div内的所有类名属性。 For Example : 例如 :

 <div class="limit-text"> <h1 class="h1 title-cover">Title</h1> <img src="image.jpg" class="icon-svg hide-for-print" alt="image"> <h3 class="copy-cover">Lorem Ipsum</h3> </div> 

The output should be as : limit-text, h1 title-cover, icon-svg hide-for-print, copy-cover . 输出应为: limit-text, h1 title-cover, icon-svg hide-for-print, copy-cover Getting all Class name attributes. 获取所有的类名属性。 I Tried 我试过了

$(div).attr('class')

but its returning only one (ie) limit-text 但它仅返回一个(即)限制文本

Please help me on this. 请帮我。

You can do this : 你可以这样做 :

Create an empty array to save classes. 创建一个空数组以保存类。 Then iterate on all tags inside the body which are not script tags. 然后遍历体内不是脚本标签的所有标签。

Get classes one by one a push them into the array created a the first line. 逐个获取类,将它们推入数组创建的第一行。

Then log the classes. 然后记录课程。

 var classes = []; $('body *:not(script)').each(function(){ _classes = $(this).attr('class') ? $(this).attr('class').split(' ') : [] _classes.forEach(function(entry){ if(classes.indexOf(entry) < 0){ classes.push(entry) } }) }) console.log(classes) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="limit-text"> <h1 class="h1 title-cover">Title</h1> <img src="image.jpg" class="icon-svg hide-for-print" alt="image"> <h3 class="copy-cover">Lorem Ipsum</h3> </div> 

With

$(div).attr('class')

you can get the class name of the parent div - this one you have already. 您可以获得父div的类名称-该名称已经存在。

Using: 使用:

$(div).find('[class]').toArray().map(child => $(child).attr('class'))

or 要么

$(div).find('[class]').toArray().map(child => child.className)

you can get an array of all children classes. 您可以获得所有子类的数组。

["h1 title-cover", "icon-svg hide-for-print", "copy-cover"]

Use children() and loop through each of them to get classnames 使用children()并遍历它们中的每一个以获得类名

 $('.limit-text').children().andSelf().each(function(){ console.log($(this).attr('class')) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="limit-text"> <h1 class="h1 title-cover">Title</h1> <img src="image.jpg" class="icon-svg hide-for-print" alt="image"> <h3 class="copy-cover">Lorem Ipsum</h3> </div> 

 var div= document.getElementsByTagName('div')[0].getAttribute("class"); var h1 = document.getElementsByTagName('h1')[0].getAttribute("class"); var img = document.getElementsByTagName('img') [0].getAttribute("class"); var h3 = document.getElementsByTagName('h3') [0].getAttribute("class"); var result = document.getElementById('result').innerHTML = `${div}, ${h1}, ${img} ,${h3}`; //or console.log(result) 
 <div class="limit-text"> <h1 class="h1 title-cover">Title</h1> <img src="image.jpg" class="icon-svg hide-for-print" alt="image"> <h3 class="copy-cover">Lorem Ipsum</h3> </div> <p id='result'></p> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM