How to select an element's parent and the parent's siblings

后端 未结 7 1423
花落未央
花落未央 2020-12-08 12:53

I have this code:

$(\"#test\").siblings(\'p\').remove();
$(\"#test\").remove();

How can I chain this code instead of writing it separately?

7条回答
  •  遥遥无期
    2020-12-08 13:33

    I have to agree with the comment on the question; for readability and maintainability, use andSelf:

    $("#test").siblings('p').andSelf().remove();
    

    Unfortunately, it's been deprecated. But if you're stuck on an older version of jquery like we are, it may be worthwhile.

提交回复
热议问题