【发布时间】:2017-01-02 13:52:49
【问题描述】:
所以我有一些有效的代码,但我知道这不是正确的方法。我知道必须有一个更简单的方法。
我有如下链接
<a href="#1"> <strong class="active">1</strong></a>
<a href="#2"> <strong class="active">2</strong></a>
<a href="#3"> <strong class="active">3</strong></a>
<a href="#4"> <strong class="active">4</strong></a>
这些链接中的每一个都激活标签内容,效果很好,所以没有问题。
我需要能够直接给人们一个链接来说出标签内容 3. 所以我想我可以使用 window.location.hash 所以我做了以下事情。
if ( window.location.hash ==="#1") {
jQuery(".slides").find('.dslc-modules-area').addClass('hide');
jQuery(".slides").find('.dslc-modules-area').eq(0).addClass('show').removeClass('hide');
jQuery( ".slidelinks li strong" ).removeClass('active');
jQuery( ".slidelinks li strong" ).eq(0).addClass('active');
}
else if( window.location.hash ==="#2"){
jQuery(".slides").find('.dslc-modules-area').addClass('hide');
jQuery(".slides").find('.dslc-modules-area').eq(1).addClass('show').removeClass('hide');
jQuery( ".slidelinks li strong" ).removeClass('active');
jQuery( ".slidelinks li strong" ).eq(1).addClass('active');
}
else if( window.location.hash ==="#3"){
jQuery(".slides").find('.dslc-modules-area').addClass('hide');
jQuery(".slides").find('.dslc-modules-area').eq(2).addClass('show').removeClass('hide');
jQuery( ".slidelinks li strong" ).removeClass('active');
jQuery( ".slidelinks li strong" ).eq(2).addClass('active');
}
else if( window.location.hash ==="#4"){
jQuery(".slides").find('.dslc-modules-area').addClass('hide');
jQuery(".slides").find('.dslc-modules-area').eq(3).addClass('show').removeClass('hide');
jQuery( ".slidelinks li strong" ).removeClass('active');
jQuery( ".slidelinks li strong" ).eq(3).addClass('active');
}
else if( window.location.hash ==="#5"){
jQuery(".slides").find('.dslc-modules-area').addClass('hide');
jQuery(".slides").find('.dslc-modules-area').eq(4).addClass('show').removeClass('hide');
jQuery( ".slidelinks li strong" ).removeClass('active');
jQuery( ".slidelinks li strong" ).eq(4).addClass('active');
}
else if( window.location.hash ==="#6"){
jQuery(".slides").find('.dslc-modules-area').addClass('hide');
jQuery(".slides").find('.dslc-modules-area').eq(5).addClass('show').removeClass('hide');
jQuery( ".slidelinks li strong" ).removeClass('active');
jQuery( ".slidelinks li strong" ).eq(5).addClass('active');
}
else if( window.location.hash ==="#7"){
jQuery(".slides").find('.dslc-modules-area').addClass('hide');
jQuery(".slides").find('.dslc-modules-area').eq(6).addClass('show').removeClass('hide');
jQuery( ".slidelinks li strong" ).removeClass('active');
jQuery( ".slidelinks li strong" ).eq(6).addClass('active');
}
else if( window.location.hash ==="#8"){
jQuery(".slides").find('.dslc-modules-area').addClass('hide');
jQuery(".slides").find('.dslc-modules-area').eq(7).addClass('show').removeClass('hide');
jQuery( ".slidelinks li strong" ).removeClass('active');
jQuery( ".slidelinks li strong" ).eq(7).addClass('active');
}
这完全可以根据需要工作,但似乎太麻烦了。
有没有办法获取散列的 index() ?所以说如果它是#2,或者#3只使用索引然后我可以使用eq(“无论索引是什么”)。 我知道需要发生什么,只是不确定语法是什么。我还想我可以获取哈希值并将其转换为数值,所以如果 #2 我又可以在 eq() 中使用它
【问题讨论】:
-
您可以用 5、6 行代码构建该代码。