[英]Selecting closest class and removing it with jQuery
当用户单击“编辑”时,如何找到最接近的row_form
并使用jquery将其删除?
以下是我试过到目前为止的jsfiddle
的HTML
<div id="settings_wrapper">
<h1>General settings</h1>
<div class="settings_row">
<span class="row_name">Name</span>
<div class="row_edit"><p class="row_edit_button">Edit</p></div>
<div class="row_form">
<form action="this.php"><span>New name:</span><input type="text" /><input type="submit" value="Save"><span>New name:</span><input type="text" /></form>
</div>
</div>
<div class="settings_row">
<span class="row_name">Name</span>
<div class="row_edit"><p class="row_edit_button">Edit</p></div>
<div class="row_form">
<form action="this.php"><span>New name:</span><input type="text" /><input type="submit" value="Save"><span>New name:</span><input type="text" /></form>
</div>
</div>
</div>
JQUERY
$(document).ready(function(){
$(".row_edit_button").click(function(){
$(this).closest(".row_form").remove();
});
});
目标元素是被单击元素的parentNode
的下一个同级。 closest
选择最匹配的父元素。 一种选择是:
$(".row_edit_button").click(function() {
$(this.parentNode).siblings(".row_form").remove();
});
其他选项是:
$(this).parent().next(".row_form").remove();
$(this).closest('.settings_row').find(".row_form").remove();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.