gpt4 book ai didi

jQuery 'and' 'or' 选择器中的条件语法?

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

我知道在 php 中 || 是一个“or”运算符,但如何在 jQquery 中使用它?

if ($("#shift1" || "#shift2" || "#shift3").is(":checked")){
$('#mon').css({
'background-color': '#88CC44',
'color':'white'
});
} else {
$('#mon').css({
'background-color': 'grey',
'color':'white'
});
}

如果检查了 #shift1#shift2#shift3 中的任何一个,我希望此功能有效,但到目前为止它仅适用于id 在列表中选择第一个。这是如何在 jQuery 中完成的,以及用“and”而不是“or”来表示 && 吗?

最佳答案

|| 也是 JavaScript 中的“or”运算符 - 但在选择器中它不适用于该语法。 jQuery 选择器使用 CSS 语法,因此要选择多个元素(也称为“或”操作),请用逗号分隔它们:

$("#shift1, #shift2, #shift3")

另请注意,您可以使用三元来简化和干燥代码:

$('#mon').css({
'background-color': $("#shift1, #shift2, #shift3").is(":checked") ? '#88CC44' : 'grey',
'color':'white'
});

关于jQuery 'and' 'or' 选择器中的条件语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33345220/

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