jQuery:删除通过 ajax 加载的 DOM 元素
我正在使用 jQuery 将 ajax 内容加载到页面上。然而,尝试使用 jQuery 的 .remove() 函数似乎无法删除动态加载的元素。
我的示例函数:
function deletePerson(personID){
if (confirm("Are you sure you want to permanently delete this person?\nYou cannot undo this action.")){
$.post("ajax/personsDelete.php", { personNo: personID },
function(data){
$('#person' + personID).remove();
alert("Response: " + data);
});
}
}
如何使用 jQuery 删除动态加载的元素?
I'm using jQuery to load ajax content on to a page. However, trying to use jQuery's .remove()
function doesn't seem to be working to remove the dynamically loaded element.
My example function:
function deletePerson(personID){
if (confirm("Are you sure you want to permanently delete this person?\nYou cannot undo this action.")){
$.post("ajax/personsDelete.php", { personNo: personID },
function(data){
$('#person' + personID).remove();
alert("Response: " + data);
});
}
}
How can I remove dynamically loaded elements with jQuery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改选择器的上下文以查看响应,添加
,data
:由于它尚未添加到文档中,因此您调用的默认选择器将找不到它:
$('#person' + personID)
与$('#person' + personID, document)
相同Change the context of the selector to look at the response, adding
,data
:Since it isn't added to the document yet, the default selector you're invoking won't find it:
$('#person' + personID)
is the same as$('#person' + personID, document)