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

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

问题描述

给定一个 div,例如:

Given a div such as:

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

我想成为abe使用JQUERY的$(div).attr('class');

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

查找所有类,但 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?

谢谢

推荐答案

你目前的工作,这是一个快速演示:

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

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

Something to keep in mind is that .attr() returns that attribute on the first matched element, from the docs:

请务必注意,.attr() 方法仅获取匹配集中的 first 元素的属性值.要单独获取每个元素的值,我们需要依赖循环构造,例如 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",因为这是 first 匹配元素的类.

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

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

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