jquery删除不处理动态创建的div的父级
我正试图使用jquery使按钮删除其父div 我的标记:jquery删除不处理动态创建的div的父级,jquery,html,dynamic,Jquery,Html,Dynamic,我正试图使用jquery使按钮删除其父div 我的标记: <div class="web_store_fields" id="web_store_input1"> <p> <label for="web_store_address[]" >Store Address : </label> <input name="web_store_address[]" class="web_store_info" type="text"
<div class="web_store_fields" id="web_store_input1">
<p>
<label for="web_store_address[]" >Store Address : </label>
<input name="web_store_address[]" class="web_store_info" type="text" value="http://www." size="35"/>
<input class="button_remove_web_store" type="button" value="Remove" />
</div>
当页面加载时,这对于html中的div是正常的,但对于
用户动态创建的div(使用此jquery代码):
需要明确的是,动态创建工作正常,问题在于删除那些div
任何提示都将受到高度赞赏使用委托处理程序()和类作为选择器,以便以后添加的任何新按钮仍能工作
$('body').on('click', '.button_remove_web_store', function() {
$(this).parents("div:first").remove();
});
不要使用处理程序——它在jQuery 1.7中被弃用,在jQuery 1.9中被删除了——这对很多人来说是一个突破性的变化 相反,使用处理程序,这是现在推荐的方法:
$(document).on('click', '.button_remove_web_store', function() {
$(this).parents("div:first").remove();
});
我最近遇到了一个与此应用程序不清楚且非常相似的问题,因为大多数“删除此父项”函数都是 用这个问题的例子来说明:
$('.web_store_fields').on('click', '.button_remove_web_store', function(e){
$(this).parent().remove();
});
这里的大问题是第二个参数。必须选择容器$(“.container”)
,并将“button”的元素选择器作为第二个参数传递给.on
函数。(某种)定义
非常感谢你,它起作用了!我是一个jquery新手,显然有很多我不知道的东西..“live”在jquery 1.7中被弃用,在jquery 1.9中被删除--不应该再使用它了。
$(document).on('click', '.button_remove_web_store', function() {
$(this).parents("div:first").remove();
});
$('.web_store_fields').on('click', '.button_remove_web_store', function(e){
$(this).parent().remove();
});
$('.container').on('click', '.myremovebutton', function(e){
$(this).parent().remove();
});