Insert after parent of element

2019-04-29 04:14发布

What sort of selector would be I need in order to insert after the parent (divouter) of the test3 class in the example below? Thanks.

<div class='divouter'>
    <div class='divinner'>
        <input class=test1></input>
    </div>
</div>
<div class='divouter'>
    <div class='divinner'>
        <input class=test2></input>
    </div>
</div>
<div class='divouter'>
    <div class='divinner'>
        <input class=test3></input>
    </div>
</div>
<div class='divouter'>
    <div class='divinner'>
        <input class=test4></input>
    </div>
</div>

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-04-29 04:57
.divouter:parent
查看更多
干净又极端
3楼-- · 2019-04-29 05:12

I think you want the "after()" method:

$('input.test3').focus(function() {
   $(this).closest('div.divouter').after('<div>Hi!</div>');
});
查看更多
神经病院院长
4楼-- · 2019-04-29 05:17

You could use the $.after() method:

$(".test3").closest(".divouter").after("<div>Foo</div>");

Or the $.insertAfter() method:

$("<div>Foo</div>").insertAfter( $(".test3").closest(".divouter") );
查看更多
登录 后发表回答