gpt4 book ai didi

jQuery 选项卡 - 从 URL 直接转到特定选项卡

转载 作者:行者123 更新时间:2023-12-01 03:17:34 24 4
gpt4 key购买 nike

我正在使用 jQuery 选项卡实现,一切都很好,但是我希望能够链接到 URL,例如 http://www.mydomain.com/tabs.html#tab2并让页面在选项卡 2 上自动打开,这是我目前所在的位置 http://jsfiddle.net/Jmx7k/

<script>
jQuery(document).ready(function() {
jQuery('#tabs2 li a:not(:first)').addClass('inactive');
jQuery('.container:not(:first)').hide();

jQuery('#tabs2 li a').click(function() {
var t = jQuery(this).attr('href');
if (jQuery(this).hasClass('inactive')) { //added to not animate when active
jQuery('#tabs2 li a').addClass('inactive');
jQuery(this).removeClass('inactive');
jQuery('.container').hide();
jQuery(t).fadeIn('slow');
}
return false;
}) //end click
});​
</script>
<div id="tabs2holder">
<ul id="tabs2">
<li><a href="#tab1">Test Tab 1</a></li>
<li><a href="#tab2">Test Tab 2</a></li>
</ul>
</div>

<div class="container" id="tab1">
This is test content for tab1
</div>


<div class="container" id="tab2">
This is test content for tab2
</div>

有人可以指出我添加此功能的正确方向,并解释为什么它目前不这样做吗?

谢谢

最佳答案

好的,如果您想创建带有哈希部分的网址(例如“#tab2”),您可以通过以下方式获取该值

var hash = location.hash; // hash = '#tab2'

像这样改变你的代码

jQuery(document).ready(function() {
jQuery('#tabs2 li a:not(:first)').addClass('inactive');
jQuery('.container:not(:first)').hide();

jQuery('#tabs2 li a').click(function() {
var t = jQuery(this).attr('href');
if (jQuery(this).hasClass('inactive')) { //added to not animate when active
jQuery('#tabs2 li a').addClass('inactive');
jQuery(this).removeClass('inactive');
jQuery('.container').hide();
jQuery(t).fadeIn('slow');
}
return false;
}); //end click

if (location.hash == '#tab2') {
// don't forget to put id-attributes to your li-elements
jQuery('#tablink2 a').trigger('click');
}
});​

另请参见此处:http://jsfiddle.net/Jmx7k/8/通过 jsfiddle,哈希属性不会影响 javascript 区域:-( 在正常上下文中尝试。

关于jQuery 选项卡 - 从 URL 直接转到特定选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13924718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com