使我的jquery选项卡式浏览更加智能
Smarten up my jquery tabbed browsing
我整理了一个非常简单的选项卡式浏览器,你可以在这里看到http://jsfiddle.net/zandergrin/sN2Eu/
我只是想知道是否有办法使脚本更智能,以便自动处理所有编号 - 即我可以添加一个新选项卡,而不需要添加新的 javascript。
我知道我可以用jquery ui做到这一点,但是a)我正在努力保持它的超轻量级,更重要的是,b)我正在努力学习!!
谢谢 - 我对我的 javascript 非常基本,所以任何解释将不胜感激
您需要
为每个选项卡添加一个 comman 类,以便您可以选择所有选项卡和一个唯一的 id,该 id 也是链接href
中的值。还要为所有链接添加一个公共类。
<div class="tab-nav">
<ul>
<li><a href="#tab1" class="tabclick active">Overview</a></li>
<li><a href="#tab2" class="tabclick">Specs</a></li>
<li><a href="#tab3" class="tabclick">More Info</a></li>
</ul>
</div>
<div id="tab1" class="tab">
<p>content1</p>
</div>
<div id="tab2" class="tab" style="display: none">
<p>content2</p>
</div>
<div id="tab3" class="tab" style="display: none">
<p>content3</p>
</div>
你的JavaScript现在可以
function tabActions(e) {
// e.preventDefault(); // stop default browser action of link will not add the hash to the url
var targetId = $(this).attr('href'); // extract the value in the href (the #tab1 for example)
$('.tabclick').removeClass('active'); // remove the active class from all links (find them by their common class)
$(this).addClass('active'); // add it to the currently clicked link
$('.tab').hide(); // find all tabs (by the common class) and hide them
$(targetId).show(); // show the current tab
};
$('.tabclick')
.click(tabActions) // bind handler
.filter(function(){ // find the link that points to the hash in the url
return this.hash == window.location.hash;
})
.click(); // manually trigger click on it
http://jsfiddle.net/gaby/sN2Eu/3/演示
相关文章:
- 如何在从浏览缓存加载页面时执行javascript
- 循环浏览多个身体背景图像
- 三星智能电视应用程序;Brightcove示例应用程序远程控制问题
- 显示浏览量最高的三篇文章
- 单击按钮时循环浏览匹配的表
- 当同一浏览器的两个实例浏览时,Javascript页面如何具有唯一的ID
- 更改智能表中的页面将停止JQuery函数的工作
- 如何在使用Javascript浏览网站时处理原始窗口
- 链接/按钮从子文件夹下载文件--浏览器只是浏览
- AngularJS智能表全局配置实现
- 创造自己的智能
- 如何在智能表中按日期对项目进行排序
- 间歇性浏览捆绑包故障(可能是brfs)
- 当我浏览回页面时,谷歌地图信息窗口不会弹出
- 如何为AngularJS制作智能表中第一页和最后一页的跳过按钮
- 三星智能电视应用程序使用谷歌API
- plupload智能手机和平板电脑浏览按钮事件未启动
- 使我的jquery选项卡式浏览更加智能
- 三星智能电视应用程序浏览历史
- 从连接的电视或智能电视进行页面浏览