使用锚点网址显示切换div

时间:2013-08-06 07:27:52

标签: javascript jquery hash toggle anchor


我使用以下脚本在我的网页上打开/关闭div;

$(document).ready(function(){

     $(".showA").hide();
     $(".show_hideB").show();

     $('.show_hideB').click(function(){
     $(".showA").slideToggle();

     });

});

一切都很好,但问题是当我上网www.websitelink.com/#A到(切换)打开锚点时,我无法让它工作#A自动

我已经尝试了以下内容,我在这里找到但是没有帮助我:

if(document.location.hash) {
$('.' + document.location.hash.substring(1)).slideDown(); }

注意:我正在以更多方式使用切换javascript,因此我正在寻找一般解决方案,而不仅仅是针对#A div(例如;如果网址说明:www。 websitelink.com#c它应该打开#c ect。等。)

我希望你们中的某个人有我的解决方案! 非常感谢先进!

1 个答案:

答案 0 :(得分:0)

您在选择器中使用.,它应该是#

if(document.location.hash) {
    $('#' + document.location.hash.substring(1)).slideDown();
}

或者,由于document.location.hash始终以#开头,因此您不需要子字符串:

if(document.location.hash) {
    $(document.location.hash).slideDown();
}
相关问题