admin管理员组文章数量:1405636
I need to check whether a table row (TR) has a class by name. So far, I have the following:
var myClass = "myClass";
//only myClass if it doesn't exist already
if (!(rowGet.className == "myClass") || !(rowGet.className == "myClass anotherClass")) {
if (rowGet) { // only add the class if TR exists
rowGet.className = myClass;
}
}
rowGet is a TR from a table. When I tried hasClass(myClass), I get an error saying HTMLTableElement has no method hasClass. Yes, I have jQuery referenced.
I need to check whether a table row (TR) has a class by name. So far, I have the following:
var myClass = "myClass";
//only myClass if it doesn't exist already
if (!(rowGet.className == "myClass") || !(rowGet.className == "myClass anotherClass")) {
if (rowGet) { // only add the class if TR exists
rowGet.className = myClass;
}
}
rowGet is a TR from a table. When I tried hasClass(myClass), I get an error saying HTMLTableElement has no method hasClass. Yes, I have jQuery referenced.
Share Improve this question edited Mar 3, 2016 at 1:56 Joseph Silber 220k59 gold badges368 silver badges292 bronze badges asked Nov 18, 2012 at 4:07 KyleKyle 5,5577 gold badges37 silver badges48 bronze badges 1-
Try
$(rowGet).hasClass("myClass");
– Derek 朕會功夫 Commented Nov 18, 2012 at 4:08
2 Answers
Reset to default 5You have to wrap your element with jQuery:
$(rowGet).hasClass('myclass');
Actually, there's no need to first check whether it already has the class applied, just use addClass
:
$(rowGet).addClass('myclass');
It won't even plain if the element doesn't exist.
Try
$(rowGet).hasClass("myClass");
本文标签: javascriptHow to check if a table row has a class name without using hasClass()Stack Overflow
版权声明:本文标题:javascript - How to check if a table row has a class name without using hasClass()? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744322984a2600593.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论