但是,这种方法确实需要在要显示的所有内容 div 元素的 id 中附加一个数字,以便识别单击的选项卡,显示的面板和选项卡外部的元素之间的关系;所以你的HTML变成:
<div id="tabs">
<ul>
<li><a href="#tab1">Tab1</a></li>
<li><a href="#tab2">Tab2</a></li>
</ul>
<div id="tab1">
test1
</div>
<div id="tab2">
test2
</div>
</div>
<div id="content1">
<p>
on click of first tab (tab1) I need to show this id as well
</p>
</div>
<div id="content2"> <!-- added the '2' to the id here -->
<p>
on click of Second tab (tab2) I need to show this id as well
</p>
</div>
如果您将内容 div 元素包装在外部容器中,在我的演示中它有 id 的 containers ,那么您可以将 div 的目标显示/隐藏略有不同:
$("#tabs").tabs({
activate: function(evt, ui) {
var num = ui.newPanel.attr('id').replace(/\D+/g, '');
$('#contents > div').eq(num - 1).show().siblings().hide();
}
});
并使用HTML:
<div id="tabs">
<ul>
<li><a href="#tab1">Tab1</a></li>
<li><a href="#tab2">Tab2</a></li>
</ul>
<div id="tab1">
test1
</div>
<div id="tab2">
test2
</div>
</div>
<div id="contents">
<div id="content1">
<p>
on click of first tab (tab1) I need to show this id as well
</p>
</div>
<div id="content2">
<p>
on click of Second tab (tab2) I need to show this id as well
</p>
</div>
</div>
function showContent(evt, ui) {
if (!evt || !ui) {
return false;
}
else {
// ui.newPanel in the activate event,
// ui.panel in the create event
var panel = ui.newPanel || ui.panel,
num = panel.attr('id').replace(/\D+/g, '');
$('#contents > div').eq(num - 1).show().siblings().hide();
}
}
$(function() {
$("#tabs").tabs({
// runs the function when the tabs are created:
create: function(evt, ui) {
showContent(evt, ui);
},
// runs the function when the tabs are activated:
activate: function(evt, ui) {
showContent(evt, ui);
}
});
});
1 回答
一种不需要将外部内容移动到选项卡本身的方法是:
JS Fiddle demo .
但是,这种方法确实需要在要显示的所有内容
div
元素的id
中附加一个数字,以便识别单击的选项卡,显示的面板和选项卡外部的元素之间的关系;所以你的HTML变成:如果您将内容
div
元素包装在外部容器中,在我的演示中它有id
的containers
,那么您可以将div
的目标显示/隐藏略有不同:并使用HTML:
JS Fiddle demo .
我修改了上面的代码,以回应OP留下的评论(下面):
JS Fiddle demo .