1、这是出问题的时候
2、这是元素的结构
3、这是js代码:
// 动态添加标签的代码
function addTab(url, name, id, closeable) {
if (hasTab(id)) {
return $('.tab-bar-tabs .tab[data-id="' + id + '"]').trigger('click');
}
var tabs = document.getElementById('tab-bar-tabs');
var btn = document.createElement('button');
btn.className += " tab btn-zbss";
btn.setAttribute('data-id', id);
var span = document.createElement('span');
span.innerText = name;
btn.appendChild(span);
if (closeable) {
var i = document.createElement('i');
i.className += " fa fa-times-circle tab-close";
btn.appendChild(i);
}
tabs.appendChild(btn);
var tabMain = document.getElementById('tab-main');
var iframe = document.createElement('iframe');
iframe.src = url;
iframe.id = id;
iframe.style.display = "none";
tabMain.appendChild(iframe);
initTabEvent();
$('.tab-bar-tabs .tab[data-id="' + id + '"]').trigger('click');
}
// 绑定事件的代码
function initTabEvent() {
$('.tab-bar-tabs .tab').unbind().click(function(e) {
$('.tab-bar-tabs .tab').each(function() {
$(this).removeClass('active');
});
$(this).addClass('active');
var id = $(this).attr('data-id');
$('.navigation li a').each(function() {
var len = $(this).parent('li').children('ul').length;
if (len < 1) {
var newId = $(this).attr('data-id');
if (newId == id) {
$(this).addClass('li-without-ul-a-active');
} else {
$(this).removeClass('li-without-ul-a-active');
}
}
});
$('.tab-main iframe').each(function() {
var iframeId = $(this).attr('id');
if (iframeId == id) {
$(this).show();
} else {
$(this).hide();
}
});
});
$('.tab-close').unbind().click(function (e) {
var evt = e || window.event;
if (evt.preventDefault) {
evt.preventDefault();
evt.stopPropagation();
} else {
evt.returnValue = false;
evt.cancelBubble = true;
}
var parent = $(this).parent('button');
if (parent.hasClass('active')) {
var prev = parent.prev();
if (prev != null && prev.length > 0) {
prev.trigger('click');
} else {
var next = parent.next();
if (next != null && next.length > 0) {
next.trigger('click');
}
}
}
var id = parent.attr('data-id');
$('.tab-main>iframe[id="' + id + '"]').remove();
parent.remove();
});
}
可能是因为i标签是动态添加的,所以没有绑定到事件,可以试试这样写