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

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

问题描述

由于一个div如:

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

我想成为安倍使用jQuery的 $(DIV).a​​ttr('类');

要找到所有的类,但JQUERY.attr只返回部​​件。有没有使用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?

感谢

推荐答案

今天我向您目前将工作,<一个href=\"http://stackoverflow.com/questions/2820916/jquerys-attr-return-multiple-classes-not-just-the-1st\">here's一个快速演示:

What you have currently will work, here's a quick demo:

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

东西要记住的是, .attr() 返回的第一个匹配元素该属性,从文档:

要注意的是, .attr()方法获取的属性值只在第一在匹配set元素是很重要的。要单独获取每个元素的值,我们需要依靠一个循环结构,如jQuery的。每()方法。

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.

因此​​,举例来说,这样的事情:

So for example, something like this:

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

就通知仅小部件,因为这是类的的第一个的匹配元素。

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

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

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