代码

HTML代码

1
2
3
4
5
6
7
8
9
<div id="menu" class="easyui-menu">
<div id="mm-tabrefresh" data-options="name:'1',iconCls:'icon-refresh'"> 刷新当前标签页</div>
<div class="menu-sep"></div>
<div id="mm-tabclose" data-options="name:'2',iconCls:'icon-closetab'">关闭当前标签页</div>
<div id="mm-tabcloseall" data-options="name:'3',iconCls:'icon-closealltab'">关闭全部标签页</div>
<div id="mm-tabcloseother" data-options="name:'4',iconCls:'icon-closeothertab'">关闭其他标签页</div>
<div id="mm-tabcloseright" data-options="name:'5',iconCls:'icon-closerighttab'">关闭右侧标签页</div>
<div id="mm-tabcloseleft" data-options="name:'6',iconCls:'icon-closelefttab'">关闭左侧标签页</div>
</div>

JavaScript代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
$(document).ready(function () {
// 监听右键事件,创建右键菜单
$('#tabs').tabs({
onContextMenu: function (e, title, index) {
e.preventDefault();
if (index > 0) {
$('#menu').menu('show', {
left: e.pageX,
top: e.pageY
}).data("tabTitle", title);
}
}
});

// 右键菜单click
$("#menu").menu({
onClick: function (item) {
closeTab(this, item.name);
}
});

function closeTab(menu, type) {
var allTabs = $("#tabs").tabs('tabs');
var allTabtitle = [];
$.each(allTabs, function (i, n) {
var opt = $(n).panel('options');
if (opt.closable)
allTabtitle.push(opt.title);
});
var curTabTitle = $(menu).data("tabTitle");
var curTabIndex = $("#tabs").tabs("getTabIndex", $("#tabs").tabs("getTab", curTabTitle));
switch (type) {
case "1": // 刷新当前标签页
var currentTab = $('#tabs').tabs('getSelected');
var url = $(currentTab.panel('options')).attr('href');
$('#tabs').tabs('update', {
tab: currentTab,
options: {
href: url
}
});
currentTab.panel('refresh');
// var panel = $("#tabs").tabs("getTab", curTabTitle).panel("refresh");
break;
case "2": // 关闭当前标签页
$("#tabs").tabs("close", curTabIndex);
return false;
break;
case "3": // 关闭全部标签页
for (var i = 0; i < allTabtitle.length; i++) {
$('#tabs').tabs('close', allTabtitle[i]);
}
break;
case "4": // 关闭其他标签页
for (var i = 0; i < allTabtitle.length; i++) {
if (curTabTitle != allTabtitle[i])
$('#tabs').tabs('close', allTabtitle[i]);
}
$('#tabs').tabs('select', curTabTitle);
break;
case "5": // 关闭右侧标签页
for (var i = curTabIndex; i < allTabtitle.length; i++) {
$('#tabs').tabs('close', allTabtitle[i]);
}
$('#tabs').tabs('select', curTabTitle);
break;
case "6": // 关闭左侧标签页
for (var i = 0; i < curTabIndex - 1; i++) {
$('#tabs').tabs('close', allTabtitle[i]);
}
$('#tabs').tabs('select', curTabTitle);
break;

}

}
});

结果截图

img