开发者

jQuery .hasClass returning false when it shouldn't be?

开发者 https://www.devze.com 2023-03-27 06:51 出处:网络
jQuery .hasClass is being invoked For reference:alias = \'2983_pirrota_1011_A20_Input%2820cyc%29_773_CEL\' which is also the id of the element.

jQuery .hasClass is being invoked

For reference: alias = '2983_pirrota_1011_A20_Input%2820cyc%29_773_CEL' which is also the id of the element.

if ($('#' + alias).hasClass('selected')) {
   alert开发者_如何学Go ("true");
} else {
   alert ("false");
}

However this is returning false when it should be returning a true since it is a member of that class!. I used the escape() function in javascript to escape special characters for the value of alias. As soon as I remove the % (which is how escape and encodeURIcomponent encode special characters) it starts working fine. Is this a known issue or is there a simple way around it?

Thanks.


Yeah, its a known thing. You need to escape special characters with 2 backslashes: http://api.jquery.com/category/selectors/


Try this

if ($('#' + alias.replace(/%/g, "\\\\")).hasClass('selected')) {
   alert ("true");
} else {
   alert ("false");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消