我有这个代码来检查元素是否具有某个类:
var p3div = $("#portlet1 #showhide");
if (p3div.attr('class').indexOf('ui-icon-plusthick') > 0) {
//do something here.
}
Run Code Online (Sandbox Code Playgroud)
但是在IE7中不支持.attr(),此代码将引发错误.有没有其他方法通过不使用attr()方法获取元素的类?
谢谢,
if($('#showhide').hasClass('ui-icon-plusthick')){
// do something
}
Run Code Online (Sandbox Code Playgroud)
hasClass()函数之外,不需要使用任何其他东西.#portlet #showhide,这会慢一些.