[英]Remove parent element jQuery
How can I remove all elements including its parent when click. 单击时如何删除所有元素,包括其父元素。 The tabs is generated dynamically. 选项卡是动态生成的。 What I tried so far is: 到目前为止,我尝试过的是:
I'm using bootbox for the confirmation. 我正在使用bootbox进行确认。
function remove() {
bootbox.confirm("Are you sure?", function(result) {
if( result != false ) {
var anchor = $(this).siblings('a');
$(anchor.attr('href')).remove();
$(this).parent().remove();
$(".nav-tabs li").children('a').first().click();
}
});
}
The tab that I will remove is generated through this: 我将删除的标签是通过以下方式生成的:
$(document).on('submit','#pop-form', function(e) {
// make an ajax request
$.post('../admin/FLT_add_tab.do',
$('#pop-form').serialize(),
function( data ) {
// if data from the database is empty string
if( $.trim( data ).length != 0 ) {
// hide popover
$('#popover').popover('hide');
//append new tab and new tab content
var id = $(".nav-tabs").children().length - 1;
$('#popover').closest('li').before('<li><a href="#tab_'+ id +'" data-toggle="tab" class="g-tabs">' + data + ' </a></li>');
$('.tab-content').append('<div class="tab-pane" id="tab_' + id + '"> <c:import url="flt-pis.html"></c:import> </div>');
} else {
// error handling later here
}
}
);
e.preventDefault();
});
UPDATE: 更新:
remove()
is called by appending <i>
when the user hover the tab 当用户将鼠标悬停在选项卡上时,通过附加<i>
来调用remove()
$(document).ready( function() {
$(document).on('mouseenter', 'a.g-tabs', function() {
$( this ).append( $('<i class="icon-clear-remove" onClick="tabRemove();"></i>') );
});
$(document).on('mouseleave', 'a.g-tabs', function() {
$( this ).find( ".icon-clear-remove:last" ).remove();
});
});
The JS that concern if the page is refreshed 与页面刷新有关的JS
<!-- Other tabs from the database -->
<c:forEach var="tabNames" items="${ allTabs }">
<li><a href="#tab_${ tabNames.value }" data-toggle="tab" class="g-tabs">${ tabNames.key } </a></li>
</c:forEach>
Popover for adding new tab 弹出框,用于添加新标签
<!-- Add new tab -->
<li><a href="#" id="popover">New <i class="icon-plus-sign"></i></a></li>
The main problem is remove
method does not know on which element it is getting called, I've changed the remove to use jQuery handler instead of inlined click handler. 主要问题是remove
方法不知道在哪个元素上调用它,我将remove更改为使用jQuery处理程序而不是内联的click处理程序。
Try 尝试
$(document).on('mouseenter', 'a.g-tabs', function () {
$(this).append($('<i class="icon-clear-remove"></i>'));
});
$(document).on('mouseleave', 'a.g-tabs', function () {
$(this).find(".icon-clear-remove:last").remove();
});
jQuery(function () {
$(document).on('click', '.icon-clear-remove', function () {
var $this = $(this);
bootbox.confirm("Are you sure?", function (result) {
if (result != false) {
alert('delete:' + $this[0].tagName)
var $a = $this.closest('.g-tabs');
alert($a.length)
$($a.attr('href')).remove();
$a.parent().remove();
$(".nav-tabs li").children('a').first().click();
}
});
})
})
I can't tell exactly how you are implementing this but if my guess is right this should work. 我不能确切地说出您是如何实现的,但是如果我的猜测是正确的,那应该可行。 If it doesn't work, you should consider setting up a fiddle. 如果它不起作用,则应考虑设置小提琴。
function remove() {
bootbox.confirm("Are you sure?", function(result) {
if (result) { $(this).remove(); }
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.