使 Twitter Bootstrap 选项卡持久化的最佳方法 [英] Best way to make Twitter Bootstrap tabs persistent
本文介绍了使 Twitter Bootstrap 选项卡持久化的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使这些标签持久存在的最佳方法是什么?
What is the best way of making these tabs persist?
http://twitter.github.com/bootstrap/javascript.html#tabs
添加一些上下文,这是针对 Ruby on Rails 应用程序.我将数组 [tab1, tab2] 传递给我的视图,渲染两个选项卡并使用 Bootstrap 选项卡插件来切换它们的可见性.
To add some context, this is for a Ruby on Rails application. I'm passing an array [tab1, tab2] to my view, rendering both tabs and using the Bootstrap tab plugin to toggle their visibility.
推荐答案
此代码根据#hash 选择正确的选项卡,并在单击选项卡时添加正确的#hash.(这里使用 jQuery)
This code selects the right tab depending on the #hash and adds the right #hash when a tab is clicked. (this uses jQuery)
在 CoffeeScript 中:
In CoffeeScript:
$(document).ready ->
if location.hash != ''
$('a[href="'+location.hash+'"]').tab('show')
$('a[data-toggle="tab"]').on 'shown', (e) ->
location.hash = $(e.target).attr('href').substr(1)
或者在 JavaScript 中:
Or in JavaScript:
$(document).ready(function() {
if (location.hash !== '') $('a[href="' + location.hash + '"]').tab('show');
return $('a[data-toggle="tab"]').on('shown', function(e) {
return location.hash = $(e.target).attr('href').substr(1);
});
});
这篇关于使 Twitter Bootstrap 选项卡持久化的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文