多个选择器包含回$(this)?

时间:2013-09-08 12:45:10

标签: jquery listitem

我不确定这是否可行。选择多个元素很容易,但今天我怀疑这是否可能:

$(this).closest("li, + $(this)").css('text-decoration','line-through');

以上代码肯定无法正常工作。这只是想法的演示。我想点击整个li(文字和复选框),因此我选择了licheckbox

6 个答案:

答案 0 :(得分:3)

$(this).closest("li").andSelf().css('text-decoration','line-through');

$(this).closest("li").addBack().css('text-decoration','line-through');

用于jQuery 1.8及以上版本

答案 1 :(得分:1)

.add()

$(this).closest("li").add(this).css('text-decoration','line-through');

答案 2 :(得分:1)

最干净的解决方案是使用专用于此用途的addBack

$(this).closest("li").addBack().css('text-decoration','line-through');

答案 3 :(得分:1)

您可以使用.add()功能添加选择器:

$(this).closest("li").add(this).css('text-decoration','line-through');

答案 4 :(得分:0)

使用andSelf

  

描述:将堆栈上的前一组元素添加到   目前的设定。

代码:

$(this).closest("li").andSelf().css('text-decoration','line-through');

我现在在以下文档中阅读:

  

注意:此函数已被弃用,现在是别名   .addBack(),应该与jQuery 1.8及更高版本一起使用。

如果您使用的是jQuery 1.8或更高版本,请使用addBack

代码:

$(this).closest("li").addBack().css('text-decoration','line-through');

答案 5 :(得分:0)

我假设你的$(this)代表复选框。如果它在li里面,你可以使用

$(this).parent('li').css('text-decoration','line-through');

这将包含完整的li以及复选框。

$(this).closest("li").andSelf().css('text-decoration','line-through');
相关问题