鉴于如下div:
<div class="widget block dock clock">yada yada</div>
我想成为使用JQUERY的$(div).attr(‘class’);
查找所有类,但JQUERY.attr只返回小部件.有没有办法让JQUERY返回为元素定义的所有类?
谢谢
解决方法
您目前的工作是什么,here’s a quick demo:
alert($("div").attr("class")); //alerts "widget block dock clock"
需要记住的是,.attr()
从文档中返回第一个匹配元素上的该属性:
It’s important to note that the
.attr()@H_403_21@ 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()@H_403_21@ method.
例如,像这样:
<div class="widget">yada yada</div> <div class="widget block dock clock">yada yada</div>
只会警告“小部件”,因为这是第一个匹配元素的类.