jquery remove removing from another element

前端 未结 4 1048
清酒与你
清酒与你 2021-01-23 15:58

According to here, jquery\'s remove function should work like so..

$(\'div\').remove(\'selector\'); 

Which I\'m trying in this example.

4条回答
  •  旧巷少年郎
    2021-01-23 16:21

    You've misunderstood what the documentation is saying. It's not looking for elements that are descendants of the matched elements that match the selector, it's simply filtering down the set of already matched elements to those that match the selector, and then removing them.

    If you have this HTML:

    Some text
    Some more text
    Some unwanted text

    and then executed this jQuery:

    $('div').remove('.unwanted');
    

    then it would only remove that third

    (the one with the unwanted class on it), because it first selects all
    elements, and then only removes those that match the selector.

    Example jsFiddle

提交回复
热议问题