jQuery的attr - 返回多个类不仅仅是第一 [英] jQuery's attr - return multiple classes not just the 1st

查看:153
本文介绍了jQuery的attr - 返回多个类不仅仅是第一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个div,例如:

Given a div such as:

<div class="widget block dock clock">yada yada</div>

我想要使用JQUERY的 $(div)。 attr('class');

I'd like to be abe to use JQUERY's $(div).attr('class');

来查找所有的类,但是JQUERY.attr只返回widget。 JQUERY是否有返回所有为该元素定义的类的方法?

to find all the classes, but the JQUERY.attr only return widget. Is there a way with JQUERY to return all the classes defined for the element?

谢谢

推荐答案

您目前的工作方式,这里是一个快速演示

alert($("div").attr("class"));​ //alerts "widget block dock clock"

要记住的一点是, .attr() 第一个匹配的元素


请务必注意, .attr 方法仅获取匹配集中第一元素的属性值。要单独获取每个元素的值,我们需要依赖一个循环结构,例如jQuery的 .each()方法。

It's important to note that the .attr() method gets the attribute value for only the first element in the matched set. To get the value for each element individually, we need to rely on a looping construct such as jQuery's .each() method.

例如,像这样:

<div class="widget">yada yada</div>
<div class="widget block dock clock">yada yada</div>​

只会提醒widget,因为这是第一个匹配元素的类。

Would alert only "widget", since that's the class of the first matching element.

这篇关于jQuery的attr - 返回多个类不仅仅是第一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆