Javascript 使用锚url显示切换div

Javascript 使用锚url显示切换div,javascript,jquery,hash,toggle,anchor,Javascript,Jquery,Hash,Toggle,Anchor,我正在使用以下脚本打开/关闭我网页上的div $(document).ready(function(){ $(".showA").hide(); $(".show_hideB").show(); $('.show_hideB').click(function(){ $(".showA").slideToggle(); }); }); 一切都很好,但问题是,当我浏览www.websitelink.com/#A打开锚定div#A时,我无法让


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

$(document).ready(function(){

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

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

     });

});
一切都很好,但问题是,当我浏览www.websitelink.com/#A打开锚定div#A时,我无法让它工作

我已经尝试了以下方法,我在这里发现了这些方法,但没有帮助我:

注意:我正在以更多的方式使用切换javascript,因此我正在搜索一个通用的解决方案,而不仅仅是针对#a div(例如,如果url上显示:www.websitelink.com#c,它应该打开#c etc.ect)

我希望你们中有人能给我一个解决办法

提前向你表示衷心的感谢

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

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

if(document.location.hash) {
    $(document.location.hash).slideDown();
}

假设您的页面上有一个CSS类名与提供的
document.location.hash
相对应的元素,并且如果在加载文档后调用了(document.location.hash)(例如
$(document.ready
函数),您的代码将以
开头
if(document.location.hash) {
    $('#' + document.location.hash.substring(1)).slideDown();
}
if(document.location.hash) {
    $(document.location.hash).slideDown();
}