问题
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