未加星标

jQuery Tabs Tutorial

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

Tabs are easy to implement and can be built to work with your existing markup. This guide will walk through the process.

December 23rd, 2011

Writing the Markup

Lets start by writing our markup as if our visitor doesn't even have javascript enabled. Even if you do not wish to support users without JavaScript, it is still a good pattern to follow to exercise separation of concerns .

<ul class='tabs'> <li><a href='#tab1'>Tab 1</a></li> <li><a href='#tab2'>Tab 2</a></li> <li><a href='#tab3'>Tab 3</a></li> </ul> <div id='tab1'> <p>Hi, this is the first tab.</p> </div> <div id='tab2'> <p>This is the 2nd tab.</p> </div> <div id='tab3'> <p>And this is the 3rd tab.</p> </div>

I used fragment identifiers ( #tab1 , #tab2 , #tab3 ) for the href values in the navigation. Each element containing a tab's content is given an ID that corresponds to a fragment identifier. This way the links are semantic and continue to be functional even if the visitor has JavaScript disabled.

JavaScript provides direct access to the fragment identifier, or hash, for anchor element objects and the window.location object by using their hash property.

Writing the jQuery

Rather than describing the code, I am just going to include it with comments that explain each step.

$('ul.tabs').each(function(){ // For each set of tabs, we want to keep track of // which tab is active and its associated content var $active, $content, $links = $(this).find('a'); // If the location.hash matches one of the links, use that as the active tab. // If no match is found, use the first link as the initial active tab. $active = $($links.filter('[href="'+location.hash+'"]')[0] || $links[0]); $active.addClass('active'); $content = $($active[0].hash); // Hide the remaining content $links.not($active).each(function () { $(this.hash).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.hash); // Make the tab active. $active.addClass('active'); $content.show(); // Prevent the anchor's default click action e.preventDefault(); }); });

See the complete document . I used jQuery's .on() method for event binding, so be sure to use jQuery 1.7 or higher.

Hey,

Follow me on Twitter and Github , that's where I'm most active these days. I welcome email (), but I'm afraid I no longer have time to answer personal requests for help regarding my plugins or posts. Thanks!

Follow @jacklmoore

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: jQueryJavaScriptJavaGitTwitter
分页:12
转载请注明
本文标题:jQuery Tabs Tutorial
本站链接:http://www.codesec.net/view/482662.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(25)