我有以下由 foreach 循环动态创建的 html 结构,并试图通过从 (ACTIVE HYPERLINK) 访问它来删除整个元素。我尝试了很多不同的方式,但无法达到它。由于整个 block (ACTIVE HYPERLINK)被重复,我认为使用超链接的类名是没有意义的。我也尝试过使用 a.active 但似乎没有用。
@foreach (var file in Model.FileAttachments)
{
<li class="aaa">
<div class="bbb">
<div class="ccc">
<div class="ddd">
<div class="eee">
<ul class="fff">
<li>
<a class="xxx" href="javascript:void(0);" data-id="@file.Id" data-toggle="confirmation" ></a> <!-- ACTIVE HYPERLINK -->
</li>
</ul>
</div>
</div>
</div>
</div>
</li>
}
<script>
$('.xxx').click(function (e) {
e.preventDefault();
$('[data-toggle="confirmation"]').confirmation('show');
});
$('[data-toggle="confirmation"]').confirmation({
//code omitted for brevity
onConfirm: function () { deleteRecord(); }, // Set event when click at confirm button
});
function deleteRecord() {
var $ctrl = $('.xxx');
$.ajax({
//code omitted for brevity
success: function (response, textStatus, XMLHttpRequest) {
if (response.success) {
ctrl.closest('.aaa').remove();
//or
$("a.active").closest('.jFiler-item').remove();
}
});
};
</script>
这是我尝试的一些例子:
$("a.active").closest('.aaa').remove();
$(".xxx").closest('.aaa').remove();
$(this).data('.aaa')remove();
$("a.active").parents('li').eq(2)remove();
$(".xxx").parents('li').eq(2)remove();
有什么想法吗?
最佳答案
更新的回复
$('.xxx').click(function (e) {
var $this = $(this);
e.preventDefault();
$('[data-toggle="confirmation"]').confirmation({
//code omitted for brevity
onConfirm: function () {
deleteRecord($this); // send reference to delete method
}
});
$('[data-toggle="confirmation"]').confirmation('show');
});
...
function deleteRecord($ctrl) {
...
原始回复
如果你想删除 block 点击<a>
元素,您需要为 click
分配一个处理程序事件:
$('.xxx').on('click', function(){
$(this).closest('.aaa').remove();
});
关于javascript - 无法使用 jQuery 获取父元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39964831/