【问题标题】:ajax hash navigation is not working correctlyajax 哈希导航无法正常工作
【发布时间】:2013-07-12 16:23:27
【问题描述】:

我想使用散列进行导航,但我的脚本会在每次页面加载时将初始散列重置为#home,无论我在 url 中添加什么散列:

这是测试散列是否存在以及在#content 中加载什么的脚本:

$page = $('#content');
if(window.location.hash == 'home') {        
    $xmlFile = 'xml/home.xml';
    $("#content").createHomeEntry();
} else if(window.location.hash == 'news') {
    $xmlFile = 'xml/news.xml';
    $("#content").createNewsEntry();
} else if (window.location.hash == 'biography'){
    $xmlFile = 'xml/bio.xml';
    $("#content").createBioEntry();
} else if (window.location.hash == 'awards'){
    $xmlFile = 'xml/awards.xml';
    $("#content").createBioEntry();
} else if (window.location.hash == 'discography'){
    $xmlFile = 'xml/discography.xml';
    $("#content").createBioEntry();
}else{
    alert('this should fire off because there is no hash but it doesnt');
    $xmlFile = 'xml/home.xml';
    $("#content").createHomeEntry();
}

somoeone 可以在这里帮助我,或者告诉我为什么这个脚本将 #home 设置为默认值。

【问题讨论】:

    标签: javascript jquery ajax hash navigation


    【解决方案1】:

    window.location.hash 返回带有数字符号的散列。

    这可以使用 4 种方法中的一种来解决。我还删除了第一个不需要的 if

    方法 #1(最佳):从哈希属性中删除 #

    $page = $('#content');
    var hash = window.location.hash.substr(1);
    if(hash == 'news') {
        $xmlFile = 'xml/news.xml';
        $("#content").createNewsEntry();
    } else if (hash == 'biography'){
        $xmlFile = 'xml/bio.xml';
        $("#content").createBioEntry();
    } else if (hash == 'awards'){
        $xmlFile = 'xml/awards.xml';
        $("#content").createBioEntry();
    } else if (hash == 'discography'){
        $xmlFile = 'xml/discography.xml';
        $("#content").createBioEntry();
    }else{
        alert('this should fire off because there is no hash but it doesnt');
        $xmlFile = 'xml/home.xml';
        $("#content").createHomeEntry();
    }
    

    方法#2(最有效):使用开关和substr哈希

    $page = $('#content');
    switch (window.location.hash.substr(1)) {
        case 'news':
            $xmlFile = 'xml/news.xml';
            $("#content").createNewsEntry();
            break;
        case 'biography':
            $xmlFile = 'xml/bio.xml';
            $("#content").createBioEntry();
            break;
        case 'awards':
            $xmlFile = 'xml/awards.xml';
            $("#content").createBioEntry();
            break;
        case 'discography':
            $xmlFile = 'xml/discography.xml';
            $("#content").createBioEntry();
            break;
        default:
            alert('this should fire off because there is no hash but it doesnt');
            $xmlFile = 'xml/home.xml';
            $("#content").createHomeEntry();
            break;
    }
    

    方法#3(最简单):添加哈希

    $page = $('#content');
    if(window.location.hash == '#news') {
        $xmlFile = 'xml/news.xml';
        $("#content").createNewsEntry();
    } else if (window.location.hash == '#biography'){
        $xmlFile = 'xml/bio.xml';
        $("#content").createBioEntry();
    } else if (window.location.hash == '#awards'){
        $xmlFile = 'xml/awards.xml';
        $("#content").createBioEntry();
    } else if (window.location.hash == '#discography'){
        $xmlFile = 'xml/discography.xml';
        $("#content").createBioEntry();
    }else{
        alert('this should fire off because there is no hash but it doesnt');
        $xmlFile = 'xml/home.xml';
        $("#content").createHomeEntry();
    }
    

    方法#4(最差):使用indexOf

    $page = $('#content');
        if(window.location.hash.indexOf('news') === 1) {
            $xmlFile = 'xml/news.xml';
            $("#content").createNewsEntry();
        } else if (window.location.hash.indexOf('biography') === 1){
            $xmlFile = 'xml/bio.xml';
            $("#content").createBioEntry();
        } else if (window.location.hash.indexOf('awards') === 1){
            $xmlFile = 'xml/awards.xml';
            $("#content").createBioEntry();
        } else if (window.location.hash.indexOf('discography') === 1){
            $xmlFile = 'xml/discography.xml';
            $("#content").createBioEntry();
        }else{
            alert('this should fire off because there is no hash but it doesnt');
            $xmlFile = 'xml/home.xml';
            $("#content").createHomeEntry();
        }
    

    【讨论】:

    • 我都尝试了,但都没有解决方案。我可以给你更多的信息吗?每当我输入 url 并添加 #news 并在 url 中按回车键时,它都会恢复所有内容并将哈希更改为 #home 并加载主页内容
    • 我更新了方法 1。试试看。尝试提醒hash 并确保它的值是您所需要的。
    • @DJERock 看这里:jsfiddle.net/e4YSz/show/#awards。代码:jsfiddle.net/e4YSz
    • 我没有尝试方法二和三,但方法 1 就像一个魅力!感谢您的帮助!
    • 不客气!我只是添加了其他方法,只是为了表明有很多方法可以解决这个问题。
    【解决方案2】:

    这里

    window.location.hash == 'home'
    

    它返回的不是“home”,而是返回#home。 Substr 您的哈希,或与 # 一起使用。 并且用于调试使用alert(window.location.hash);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-16
      • 2016-08-15
      • 2012-09-26
      • 2016-06-26
      • 1970-01-01
      • 2016-03-18
      相关资源
      最近更新 更多