How to Create jQuery tabs by UI Plugin - 百姓标王 Skip to content

jQuery tab plugin

In many scenarios, you have different pieces of information related to a specific topic while it is important to present all information on one web page. For example, you might have seen in e-commerce sites product pages, that information is presented in Tabs format for features, reviews etc.

The tab feature is quite useful in that it allows you to cater more information while occupying less space, however, you must take into account the SEO perspective in view while managing tabs in your web pages.

A Tab example

The tabs can be filled by the static content or you can use the dynamic or AJAX driven content as well. There are different ways to create tabs in your web pages e.g. bootstrap, jQuery etc. however, this tutorial will deal only creating the tabs by using jQuery tab plug-in.

The jQuery, just like many other features like dialog, sliders, auto-complete etc. provides the way to create tabs by using its plugin. Creating jQuery UI tab is pretty simple that it needs only a few line of codes.

Following is the simple process to create jQuery tabs:

  • Include the jQuery library.
  • You have to include jquery UI library in web document as well.
  • CSS or style can be default as provided by jQuery or you can use as per the design of your website.
  • jQuery UI tab uses order or unordered list element i.e <ol> and <ul>.
  • The title of each tab is defined inside <li> tag of ordered or unordered lists that are wrapped by anchor tag <a>.
  • Finally, you can use div, paragraph or another tag to present Tab’s content but it must contain an ID attribute that corresponds to ID or hash of <a> tag, that contains the title (as mentioned in the last point).

Now, let us look at a few examples of using jquery tab UI plug-in.

A simple Tab example in jQuery UI

In the following example, we have created simple tabs by using Tab UI plugin in jQuery. The demo shows four tabs with one line of content. For this example, we have changed the default colors by using in-line CSS.

jquery tab basic

See online demo and code

The Markup used in this example:

<div id="tab_example" style="background:#FFFFFF;width:450px;">
<ul style="background:#846733;">
<li style="background:#FDF8E4;"><a href="#t1">Tab 1</a></li>
<li style="background:#FDF8E4;"><a href="#t2">Tab 2</a></li>
<li style="background:#FDF8E4;"><a href="#t3">Tab 3</a></li>
<li style="background:#FDF8E4;"><a href="#t4">Tab 4</a></li>
</ul>
<div id="t1" >
<p>This is Tab 1 content</p>
</div>
<div id="t2">
<p>This is Tab 2 content</p>
</div>
<div id="t3">
<p>This is Tab 3 content</p>
</div>
<div id="t4">
<p>This is Tab 4 content</p>
</div> 
</div>

 The simple tab jQuery code:

<script>
$(function() {
$( "#tab_example" ).tabs();
});
</script>

As you can see in the code, first of all, a div element with ID=tab_example is created. Within this, an unordered list <ul> with four list elements are created that represents titles of tabs. Each of these tabs contains the <a> tag with an ID. Keep this ID in mind as this will correspond in the lower div that contains the content for each tab.

Lastly, we created four div elements with same ids as we used for <a> IDs in list items.

Tab example that opens on mouse-over

In the above example, you need to click on a tab in order to open. By simply using event option with ‘mouseover’ value you can open tabs as mouse hovers over a tab. Following is a working demo of opening a tab as the mouse is over a tab.

jquery tab mouseover

See online demo and code

Not only you can use mouseover value but also other valid values related to events like mouseenter, mouseleave, dblclick etc.

If you use the dblclick value in the event option, you can open tabs by using double click. Upon the single click, the tabs will not be opened in that case.

See online demo and code

The jQuery code:

<script>
$(function() {
$( "#tab_example" ).tabs({
event: "mouseover"
});
});
</script>

 The CSS:

.ui-tabs {
position: relative;
padding: .2em;
background:#DCEAC8;
}
.ui-tabs .ui-tabs-nav {
margin: 0;
padding: .2em .2em 0;
}
.ui-tabs .ui-tabs-nav li {
list-style: none;
float: left;
position: relative;
top: 0;
margin: 1px .2em 0 0;
border-bottom-width: 0;
padding: 0;
white-space: nowrap;
background:#8EBA4E;
border-radius:9px;
}
.ui-tabs .ui-tabs-nav li.ui-tabs-active {
margin-bottom: -1px;
padding-bottom: 1px;
background:#4F6828;
}

.ui-tabs .ui-tabs-panel {
display: block;
border-width: 0;
padding: 1em 1.4em;
background: #8EBA4E;
}

In the above two examples, we overridden CSS classes provided by jQuery-UI to design the theme of Tabs. You can change it as per the need. You may get the default styles here: http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css

An example of jQuery Tabs with close option

In this example, we have created tabs with a close option. Unfortunately, we don’t have simple option provided by jQuery UI to make it closeable by adding an option. You have to handle it by a little programming. Before explaining how we added the close icon in tabs, first look at its graphic and run-able demo:

jquery tab close

See online demo and code

The code of jQuery with close option:

<script>
$(function() {
$( "#tab_example" ).tabs({
});

$('#tab_example span.ui-icon-close').on( "click", function() {
var panelId = $( this ).closest( "li" ).remove().attr( "aria-controls" );
$( "#" + panelId ).remove();
tabs.tabs( "refresh" );
});

});
</script>

 

First of all, how close icon is added? It is simply by using a span element with icon class:

<span class=’ui-icon ui-icon-close’ role=’presentation’></span>

i.e. ui-icon-close class.

Then in the script section, we used the jQuery on method where we dealt with the closing of tabs.

A vertical Tabs example

Until now we have worked with horizontal tabs that you can create quite easily and also used some options and event provided by jQuery tabs plugin. In this example, we will create vertical tabs with custom CSS.

Though there is no simple and straightforward “orientation” option to show tabs as vertical like in the case of the vertical slider that we created, yet creating vertical tabs is easy.

 

jquery tab vertcal

See online demo and code

The vertical tab code:

<script>
$(function() {
$( "#tab_example" ).tabs().addClass( "ui-tabs-vertical ui-helper-clearfix" );
$( "#tab_example li" ).removeClass( "ui-corner-top" ).addClass( "ui-corner-left" );
$( "#tab_example" ).tabs({
event: "mouseover" 
});

}); 
</script>

The CSS:

.ui-tabs-vertical { width: 55em;background:#DAE9C5; }
.ui-tabs-vertical .ui-tabs-nav { padding: .2em .1em .2em .2em; float: left; width: 12em; }
.ui-tabs-vertical .ui-tabs-nav li {background:#DAE9C5; clear: left; width: 100%; border-bottom-width: 1px !important; border-right-width: 0 !important; margin: 0 -1px .2em 0; }
.ui-tabs-vertical .ui-tabs-nav li a { display:block; }
.ui-tabs-vertical .ui-tabs-nav li.ui-tabs-active { padding-bottom: 0; padding-right: .1em; border-right-width: 1px; border-right-width: 1px; background:#4F6828;color:#FFFFFF;}
.ui-tabs-vertical .ui-tabs-panel { padding: 0.9em; float: left; width: 40em;background:#DAE9C5}

You can see in the demo and code that it used a few classes of jQuery UI plugin to create a vertical tab example. You can style it according to your theme and amend colors and width etc. Just like in the horizontal tabs demo you can use the event: “mouseover” to display tab’s content as the mouse is over a particular tab. Otherwise, just remove that line of code from demo code to make it clickable to open a tab.

A tab example with AJAX data

Not only you can present the static content in tabs of jQuery UI but you can also use DB driven or information from the text or other files by using AJAX call. This is quite easy to accomplish in jQuery tabs. All you need is to specify text/PHP etc. file in tabs and use the built-in AJAX call to retrieve and present data in the Tabs.

Following example shows how you can do it easily.

jquery tab ajax

See online demo and code

The jQuery code:

<script>
$(function() {
$( "#tab_example" ).tabs();
});
</script>

 The Markup section calls the text files:

<div id="tab_example" style="background:#FFFFFF;width:450px;">
<ul style="background:#846733;">
<li style="background:#FDF8E4;"><a href="ajax/tab1.txt">Tab 1</a></li>
<li style="background:#FDF8E4;"><a href="ajax/tab2.txt">Tab 2</a></li>
<li style="background:#FDF8E4;"><a href="ajax/tab3.txt">Tab 3</a></li>
</ul>

</div>

In the above demo, we used text files to populate tab’s content with AJAX. You may use the script files like PHP to get database’s data. You can use events like beforeLoad to manage error handling if AJAX call has some issues.

The beforeLoad event of tab widget occurs just before AJAX request is made. You can change ui.jqXHR e.g. ui.jqXHR.error to do error handling .e.g. Database connection could not be established or data was unable to load or some other message.

相关内容推荐

加盟行业网站优化比较好seo优化对企业网站的影响咸宁网站优化机构网站关键词优化培训长度对网站优化有什么影响浙江网站优化技术排名达州网站制作优化提高网站内链优化怎么自己优化网站用金苹果网站自然优化哪家价格实惠网站优化关键词是怎么做的菏泽网站优化很重要吗公司网站优化在线推广网站最新优化方式南宫网站优化推广荆门市网站做优化价格网站文件资源的优化SEO网站关键词排名优化公高港区网站优化北京网站优化有哪些淄川做网站优化的网络公司宁波网站优化报价网站优化及服务壹起航网站优化的意义攀枝花手机网站优化费用seu优化网站推广seo网站排名优化乚品达公关达州关键词网站优化荆州网站排名优化哪家好网站的优化询问j火27星图片型网站性能优化澳门网站优化哪家好昆明优化网站界面便宜的网站优化公司哪个好衡水优化网站公司吉林专业的seo网站优化费用唐山网站优化服务效果好的网站seo优化电话徐州网站关键词优化工作室大良网站优化费用网站导航如何seo优化沁阳网站自然优化网站排名优化方法+s新县网站优化的方式阜阳网站首页优化怎么做红河网站制作优化设计方案怎么对网站做seo优化做一个优化网站要多少钱对于购物网站提出优化建议山西晋城网站优化推广深圳如何把网站优化营销网站推广与优化的实现对企业网站内页优化方法网站优化公司交多少税青岛网站优化流程象山网站优化推广铜川网站优化培训兰州网站整站优化哪些平台好网站做优化躺云速捷绝伦网站内容更新属于什么优化刷360网站优化网站优化未来发展方向seo网站优化的目的和重要性百家号网站优化seo网站优化指数合肥网站优化软件河北专业网站seo优化推广搜帝网站优化费用福州推荐机械行业网站优化辽宁测量网站优化产品介绍网站优化方案的摘要优化网站推广联系方式网站前端性能优化总结如何在自己的网站优化视觉交通设备网站seo优化团队服务行业网站优化怎么实施深圳哪家网站优化及营销方案网站优化外包首荐云速捷网站排名优化 枫子科技q优化网站设计zi乚云速捷网站底部怎么优化网站优化流程和步骤合作网站自然优化南县网站seo优化排名seo对网站优化大师优化网站品牌网站建设优化公司优化网站排名方案上海正规网站优化seo价格镇江网站建设优化公司网站收录优化外链优化网站优化二级域名优缺点网站的URL如何优化常宁岳阳网站优化推广锦州网站优化权重泸州网站整站优化费用盐城出名的网站推广优化漳州网站首页优化公司网站图片优化从哪几个方面入手大连网站优化策略福田软件网站优化方法太湖网站优化制作公司咸阳企业网站排名优化黄冈网站关键字优化潜江市网站关键词优化怎么做网站视频怎么优化揭阳网站权重优化娄底网站排名优化头条优化网站网站 优化安全选云速捷使用西藏优化型网站长岛个性化网站优化成都正规的网站优化与推广乐清网站优化哪家好网站优化建设100个实例江西靠谱的网站关键词优化海南价格低的网站优化网站站内优化技术阳春英文网站seo优化移动网站优化五大注意事项如何优化网站咨询f火27星兴义网站优化价格番禺怎样优化网站建设尚堂网站设计建设优化新乡网站建设优化公司地址福永网站seo优化哪家好陆丰网站怎么优化谷歌网站优化收费多少临夏百度网站优化澄迈县湖南网站优化推广长丰网站优化制作公司荥阳网站优化报价网站优化的重要性必要性优化网站方法及云速捷声名杭州网站建设优化哪个好广饶英文网站优化哪家好濮阳网站建设优化公司地址网站排名优化图片绍兴优化网站出售优化网站方法热播易速达阜阳公司网站优化公司手机网站优化服务商镇江网站排名优化哪家好黄江做网站优化公司排名寻找邯郸网站优化服务宁乡网站优化价格陆丰网站优化费用关于企业网站的优化潮州营销型网站优化绍兴网站优化搜索贵州移动网站优化价格表密云企业网站优化推广虎丘网站推广优化抚宁区网站优化公司武昌网站优化公司服务网站优化描述字数最专业的网站优化推广软件外贸网站需要seo优化吗房山网站优化报价盐城网站优化哪家好网站优化推广怎么做最好优化网站软件申请云速捷六网站优化快速排名方法网站重复度的优化网站优化推广营销电话网站优化难在哪徐州网站优化驭明网络枣庄营销型网站优化平台网站优化的方法青岛网站类优化公司济南正宗网站优化服务供应链优化网站黄浦区谷歌网站优化聊城网站优化平台图片清晰度优化网站简阳网站优化多少钱南安网站seo优化百度网站优化遇到的问题碑林区网站优化网站优化的PPT如何把网站内链优化待优化的网站搜索引擎有实力的网站优化价格优惠快速网站优化工具达州网站制作优化旌阳区网站seo优化排名潜山网站整站优化其他网站优化公司优化网站就选火1星动漫网站url优化网站优化推广前程优化网站xm.so全面深度优化的网站英文网站怎样seo优化网站优化人员培训深圳官方网站优化的方法阿里门户网站优化昆明网站优化价格亦庄网站优化平台网络优化推广网站

合作伙伴

百姓标王

龙岗网络公司
深圳网站优化
龙岗网站建设
坪山网站建设
百度标王推广
天下网标王
SEO优化按天计费
SEO按天计费系统