从同一页面上的 URL 链接到特定的 twitter bootstrap 选项卡 [英] Linking to specific twitter bootstrap tabs from URL on same page
问题描述
我是一个 javascript 菜鸟,我想知道如何实现这个问题的答案?
I'm a javascript noob and I'm wondering how do I implement the answer to this question?
我想在标签所在的同一页面上使用此代码....
I want to use this code on the same page that the tabs are located....
<script type="text/javascript">
// Javascript to enable link to tab
var url = document.location.toString();
if (url.match('#')) {
$('.nav-tabs a[href=#'+url.split('#')[1]+']').tab('show') ;
}
// Change hash for page-reload
$('.nav-tabs a').on('shown', function (e) {
window.location.hash = e.target.hash;
})
</script>
我应该在包含选项卡的页面内的哪个位置插入它.
Where inside the page containing the tabs should I plugg this in.
再次,很抱歉我是个菜鸟.
Again, so sorry for being such a noob.
推荐答案
尝试将代码包装在 $(document).ready()
函数中
Try wrapping the code in the $(document).ready()
function
$(document).ready(function() {
//your code here...
});
您上面的内容将不起作用,因为 DOM 在运行时还没有准备好.使用 $(document).ready()
函数将延迟执行,直到 dom 加载完毕.它几乎可以出现在包含选项卡的页面中的任何位置.有些人认为它应该放在 head
部分,有些人认为它应该放在最后.但请阅读此处以获得更深入的答案:我将 $ 放在哪里(document).ready()?
What you have above won't work as the DOM won't be ready when it runs. Using the $(document).ready()
function will delay execution until the dom has loaded. It can go pretty much anywhere in the page containing the tabs. Some people think it should go within the head
section, some think it should go at the end. But read here for more in-depth answers on that: Where do I put the $(document).ready()?
参见:http://docs.jquery.com/Tutorials:Introducing_$(文档).ready()
See: http://docs.jquery.com/Tutorials:Introducing_$(document).ready()
这篇关于从同一页面上的 URL 链接到特定的 twitter bootstrap 选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!