Jquery-从不同链接加载选项卡
Jquery - Load tab from different link
我使用下面的jquery代码来创建一个简单的选项卡视图,如果您单击某个选项卡,它将显示该选项卡特定的div。这只适用于单击ul.tabs属性内的选项卡。我如何调整代码以允许在ul.tabs列表之外的同一页面上有一个链接来显示我想要的任何div?
$(document).ready(function(){
$('ul.tabs').each(function(){
// For each set of tabs, we want to keep track of
// which tab is active and it's associated content
var $active, $content, $links = $(this).find('a');
// Use the first link as the initial active tab
$active = $links.first().addClass('active');
$content = $($active.attr('href'));
// Hide the remaining content
$links.not(':first').each(function () {
$($(this).attr('href')).hide();
});
// Bind the click event handler
$(this).on('click', 'a', function(e){
// Make the old tab inactive.
$active.removeClass('active');
$content.hide();
// Update the variables with the new link and content
$active = $(this);
$content = $($(this).attr('href'));
// Make the tab active.
$active.addClass('active');
$content.show();
// Prevent the anchor's default click action
e.preventDefault();
});
});
});
这是html:
<ul class="tabs">
<li><a href="#photo_tab">Photos</a></li>
</ul>
<div id="photo_tab" class="tabcontent">
<h3>Section 1</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec lobortis placerat dolor id aliquet. Sed a orci in justo blandit commodo. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae.</p>
</div>
因此,我想在页面上其他地方的链接上使用href="#photo_tab",该链接将显示photo_tabdiv并显示活动选项卡。
您可以在正确的选项卡上手动触发点击。
HTML:
<div class="extlink" data-tabid="photo_tab">click me!</div>
<ul class="tabs">
<li><a href="#photo_tab" id="photo_tab">Photos</a></li>
</ul>
JS:
$('.extlink').click(function(){
var tabid = $(this).attr('data-tabid'); // this gets the id of the element you want to trigger
$('#'+tabid).click(); /// this acts as if you clicked on that tab element
});
相关文章:
- 过滤”;溢价;页面加载选项使用混合
- Jquery-从不同链接加载选项卡
- 如何选择页面加载选项
- Rally App SDK 2.0:rallymultiobjectpicker无法加载选项文本
- 如何在加载选项卡内容时显示加载图像
- 单击图像时从数据库加载选项id
- 如何在页面加载时自动加载选项卡内容(外部链接)
- 根据上一个选项卡的选择器重新加载选项卡
- 如何在 ui-sref 标记中放置重新加载选项
- j查询更改下拉列表调用 Web 服务以加载选项
- 在 HTML 模板的变量中使用 jQuery 查找选择以加载选项
- 使用 Ajax 调用加载选项卡内容
- 如何在重新加载选项(浏览器)时限制页面加载
- jquery ui ajax选项卡-重新加载选项卡,或更改选项卡位置
- 页面加载准备就绪时加载选项卡中的内容
- 单击“加载选项卡内容”
- 从服务器中选择加载选项
- 更改从存储加载选项的下拉dojo的字体大小
- 异步淘汰observableArray选择加载选项
- Chrome扩展:如何使用键盘事件从任何地方重新加载选项卡