需求:后台框架实现使用js在TabA中点击按钮打开或刷新TabB页面
代码示例:
1: 按钮点击事件函数 ajax 实现
// 点击按钮触发的ajax脚本
$.ajax({
url: 按钮调用的后台需处理的其他业务的url地址,
type: 'get',
data:{"id":id},
"dataType": "json",
async: false,
success: function (data) {
if (data.code !== 0){
swal({title: "注意", text: data.message, type: "error"});
} else {
swal({title: "成功", text: data.message, type: "success"});
}
page([[@{你的目标tab接口地址}]],"目标tab名称");
}
});
2: 打开目标tab的page('你的目标tab接口地址','目标tab名称')实现
function page(url, title) {
var nav = $(window.parent.document).find('.J_menuTabs .page-tabs-content .J_menuTab');
if (nav.text().indexOf('目标tab名称')) {
// 如果目标tab已打开,只做刷新动作
showTab('目标tab URL');
refreshHTab('目标tab URL');
} else {
// 如果目标tab未打开,直接打开新tab
$(window.parent.document).find('.J_menuTabs .page-tabs-content ').find(".J_menuTab.active").removeClass("active");
$(window.parent.document).find('.J_mainContent').find("iframe").css("display", "none");
var iframe = '<iframe class="J_iframe" name="iframe10000" width="100%" height="100%" src="' + url + '" frameborder="0" data-id="' + url
+ '" seamless="" style="display: inline;"></iframe>';
$(window.parent.document).find('.J_menuTabs .page-tabs-content ').append(
' <a href="javascript:;" class="J_menuTab active" data-id="'+url+'">' + title + ' <i class="fa fa-times-circle"></i></a >');
$(window.parent.document).find('.J_mainContent').append(iframe);
}
}
//刷新标签页
function refreshHTab(data_id){
var a = (window.top);
var ele = a.document.querySelectorAll('[data-id="' + data_id + '"]')[1];
ele.contentWindow.location.reload(true);
}
// 只把目标tab置为active
function showTab(dataId){
$(window.parent.document).find(".J_menuTabs .page-tabs-content .J_menuTab").each(function (i,val) {
if (val.getAttribute('data-id') == dataId) {
if (val.getAttribute('class').indexOf('active') < 0) {
val.setAttribute('class','J_menuTab active');
$(window.parent.document).find(".J_mainContent .J_iframe").each(function (j,content) {
if (content.getAttribute('data-id') == dataId) {
content.setAttribute('style','display:true');
} else {
content.setAttribute('style','display:none');
}
});
}
} else {
if (val.getAttribute('class').indexOf('active') > 0) {
val.setAttribute('class','J_menuTab');
}
}
});
}