有时候,我想在某些条件下,在链式中添加类。在语义上不添加类最合适的值是什么?
示例:
$(".element").doSomething().addClass(condition ? "special-class" : undefined).doSomethingElse();或者:
$(".element").doSomething().addClass(condition ? "special-class" : null).doSomethingElse();发布于 2015-02-21 01:06:22
使用$.toggleClass()代替:
$(".element").doSomething().toggleClass("special-class",condition).doSomethingElse();正如ruakh所提到的,这是有区别的。如果condition是错误的,它实际上将删除类,如果它是存在的。使用您的逻辑,只能添加类,而不能删除类。
发布于 2020-08-21 10:11:11
jQuery检查输入参数如下:
typeof input == "string"因此,空或未定义的的行为方式都是相同的。
https://stackoverflow.com/questions/28640825
复制相似问题