How to select an element's parent and the pare

2019-01-17 05:19发布

I have this code:

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

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

7条回答
smile是对你的礼貌
2楼-- · 2019-01-17 06:05
$("#test").siblings("p").siblings("#test").remove();
查看更多
Summer. ? 凉城
3楼-- · 2019-01-17 06:07

May be off topic.. you can change the view of the problem:

$("#test, #test ~ p").remove(); // !! incorrect way, see comment below

Guys sorry :( my decision isn't correct!!

Selector '~' isn't equal 'sibling'-method. '~' selects all sibling 'p'-elements that follow AFTER the '#test' element.

So i suggest another decision:

$("#test, > p", $('#test').parent()).remove();

It is not enough elegant but the most faster way. Please check it 'http://jsperf.com/how-to-chain-this-code-stackoverflow-16009101'

Performance test result:

Performance test result

PS http://jsfiddle.net/Dpe59/

查看更多
够拽才男人
4楼-- · 2019-01-17 06:08

Try this :

$("#test").siblings('p').addBack().remove();
查看更多
SAY GOODBYE
5楼-- · 2019-01-17 06:13

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.

查看更多
【Aperson】
6楼-- · 2019-01-17 06:13
$("#text").siblings('p').remove().prevObject.remove();

edit: Though this works, don't do this. as Matt's comment says, prevObject is undocumented

查看更多
虎瘦雄心在
7楼-- · 2019-01-17 06:16

You want to use addBack() in this case:

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

EDIT

Firstly, for future visitors, if you're using jQuery version 1.8-, you're probably need to use andSelf() which is the predecessor of addBack() for compatibility issues.

Secondly, both end and addBack will do the same task in this case but they're actually different perspective. Take a look at this HTML:

<div class="grandpa">
    <div class="dad">
        <div class="son">
            Test
        </div>
    </div>
</div>

If we're using end():

$('.grandpa')
    .find('.dad')
        .find('.son')
        .addClass('youngster')
        .end()
    .addClass('adult')
    .end()
.addClass('oldster');

The result will look like this:

<div class="grandpa oldster">
    <div class="dad adult">
        <div class="son youngster">
            Test
        </div>
    </div>
</div>  

So when we use end() for son, we're telling jQuery that it need to go back from son to parent set which is dad and add class adult.

But when we use addBack:

$('.grandpa')
    .find('.dad')
        .find('.son')
        .addClass('youngster')
        .addBack()
        .addClass('adult')
        .addBack() // This simply do nothing since `addBack` is not traverse up DOM element 
        .addClass('oldster');    

which will result in this:

<div class="grandpa">
    <div class="dad adult oldster">
        <div class="son youngster adult oldster">
            Test
        </div>
    </div>
</div>

So when we call addBack on son, we're telling jQuery to push dad and son into the same room and add new class adult and oldster to both of them.

查看更多
登录 后发表回答