javascript jQuery 在单击时删除克隆的元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15549571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery remove cloned element on click
提问by Brent
I have a script that removes the duplicated form when clicked. But when you hit remove I want to only remove the form the was cloned. I believe i need to use $this
but not sure how?
我有一个脚本,可以在单击时删除重复的表单。但是当你点击删除时,我只想删除被克隆的表单。我相信我需要使用$this
但不确定如何使用?
jQuery
jQuery
$(".remove").click(function() {
$('.duplicate').remove();
});
HTML
HTML
<div class="duplicate">
<p>Form Duplicate</p>
<a href="#" class="add">Add Guest</a> | <a href="#" class="remove">Remove </a>
</div>
<div class="duplicate">
<p>Form Duplicate</p>
<a href="#" class="add">Add Guest</a> | <a href="#" class="remove">Remove </a>
</div>
Here's a live preview: http://www.waterfrontexeter.co.uk/preordernew/.
这是实时预览:http: //www.waterfrontexeter.co.uk/preordernew/。
Thanks!
谢谢!
回答by VisioN
Just use closest()
and don't forget about preventDefault()
:
只需使用closest()
,不要忘记preventDefault()
:
$(".remove").click(function(e) {
$(this).closest(".duplicate").remove();
e.preventDefault();
});