【问题标题】:Show toggle div with anchor url显示带有锚 url 的切换 div
【发布时间】:2013-08-07 02:25:33
【问题描述】:


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

$(document).ready(function(){

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

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

     });

});

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

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

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

注意: 我正在以更多方式使用切换 javascript,因此我正在寻找通用解决方案,而不仅仅是特定于 #A div(例如;如果 url 显示:www. websitelink.com#c 它应该打开 #c ect. ect.)

我希望你们中的某个人能为我提供解决方案!
非常感谢您!

【问题讨论】:

  • 我没有看到您的代码有问题,假设您的页面上有一个 CSS 类名称与提供的 document.location.hash 对应的元素,并且您的以 if(document.location.hash) 开头的代码在文档已加载(例如在$(document).ready 函数内)

标签: javascript jquery hash toggle anchor


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    相关资源
    最近更新 更多