gpt4 book ai didi

由于多个类,jQuery 和 If 语句不匹配

转载 作者:行者123 更新时间:2023-12-03 22:56:58 25 4
gpt4 key购买 nike

我想通过添加类“noEdit”从 .click 函数中排除 DOM 元素,我遇到的问题是其中一些元素具有多个类,即:

<td class="firstCol noEdit"> // <-- wont work
<td class="noEdit"> // <-- works fine

还有 jQuery:

$('td').click( function(){
if($(this).attr('class') != "noEdit"){
alert('do the function');
});

想法?

最佳答案

如果查询class属性为 attr() ,它只是将值作为单个字符串返回。然后,您的第一个 <td> 的条件就会失败。因为您的代码将尝试比较

"firstCol noEdit" != "noEdit"

返回 true(因为它们不相等)并导致显示警报。

您需要查看 hasClass()相反,它会为您解析类列表并检查属性中是否存在给定类:

$('td').click(function() {
if (!$(this).hasClass("noEdit")) {
alert('do the function');
}
});

关于由于多个类,jQuery 和 If 语句不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3693351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com