admin管理员组文章数量:1323714
<button id="test" class="example whatever">Show classes</button>
$('button').click(function(){
var classes = $(this).attr('class');
alert(classes);
});
The above code returns the content of the class
attribute. How can I get it to return the classes formatted?
For instance, in this case, I want the variable classes
to have a value of .example.whatever
instead of example whatever
.
I've searched and the only solution that seemed to be provided is the code I demonstrated above.
<button id="test" class="example whatever">Show classes</button>
$('button').click(function(){
var classes = $(this).attr('class');
alert(classes);
});
The above code returns the content of the class
attribute. How can I get it to return the classes formatted?
For instance, in this case, I want the variable classes
to have a value of .example.whatever
instead of example whatever
.
I've searched and the only solution that seemed to be provided is the code I demonstrated above.
Share Improve this question asked Apr 29, 2012 at 3:55 UserIsCorruptUserIsCorrupt 5,03515 gold badges40 silver badges42 bronze badges2 Answers
Reset to default 6I'm really very uneasy about this, since replacing spaces with periods will give the representation of stacked classes, when the classes aren't really stacked, but here you go:
// class="foo bar fiz buz" -> .foo.bar.fiz.buz
this.className.replace( /^|\s/g , "." );
Demo: http://jsbin./awitef/edit#javascript,html
$('button').click(function(){
var classes = $(this).attr('class');
alert("." + classes.split(" ").join(".") );
});
Fiddle here: http://jsfiddle/wxDEh/3/
本文标签: javascriptGet current element39s classes in jQueryStack Overflow
版权声明:本文标题:javascript - Get current element's classes in jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742126838a2421980.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论