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
Add a ment  | 

2 Answers 2

Reset to default 5

You 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