Event to remove element before

£可爱£侵袭症+ 提交于 2019-12-11 16:56:42

问题


How would I write an event in jQuery so that if I click any of the links it'll delete not the divouter surrounding it, but the divouter before it?

    <div class='divouter'>
<a href='#'>Link</a>
    </div>
    <div class='divouter'>
<a href='#'>Link</a>
    </div>
    <div class='divouter'>
<a href='#'>Link</a>
    </div>
    <div class='divouter'>
<a href='#'>Link</a>
    </div>

回答1:


Try this:

$(".divouter a").click(function() {
    $(this).parent(".divouter").prev(".divouter:last").remove();
});



回答2:


$('.divouter a').click(function(){
    var prevParent = $(this).parent().prev();
    if (prevParent.length) prevParent.remove();
});


来源:https://stackoverflow.com/questions/2262942/event-to-remove-element-before

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!