Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 我能';t选择动态创建的元素的父元素_Jquery_Html - Fatal编程技术网

Jquery 我能';t选择动态创建的元素的父元素

Jquery 我能';t选择动态创建的元素的父元素,jquery,html,Jquery,Html,这是将列表项放在第一位的: <ul style="max-width:500px" id="currentRoles" class="list-group"> <li class="currentRole list-group-item"> <div class="col-md-6"> @role.Name </div> <a h

这是将列表项放在第一位的

<ul style="max-width:500px" id="currentRoles" class="list-group">
     <li class="currentRole list-group-item">
            <div class="col-md-6">
                @role.Name
            </div>
            <a href="#" class="linkRemoveRole">Remove This Role</a>
     </li>
</ul>
问题来了。我只想在单击“Addroleback”链接时删除它的父元素,但结果发现我做不到。我检查了控制台,似乎我不能选择它的父母

 $(this).parentsUntil('#removedRoles').remove();
但如果我这么做的话,效果很好

 $('.linkAddRoleBack').parentsUntil('#removedRoles').remove();
有人知道原因吗?

使用$(文档)。事件:

$(document).on('click','.linkAddRoleBack',function (event) {
       var roleName = $(this).siblings().text();
       $(this).parentsUntil('#removedRoles').remove();
});

如果要绑定动态元素上的事件,请使用-是否定义“添加回”单击处理程序
$('.linkAddRoleBack')。在附加html代码后单击(func…
)?我的意思是在移除单击处理程序函数`$('.linkremovole')的内部/末尾。单击(func…`。
 $('.linkAddRoleBack').parentsUntil('#removedRoles').remove();
$(document).on('click','.linkAddRoleBack',function (event) {
       var roleName = $(this).siblings().text();
       $(this).parentsUntil('#removedRoles').remove();
});