当我输入网址并按下回车键时,如何从URL中获取哈希码?

3
我将翻译如下:

我有这个网址:example.com/index.html#tab-1

我在浏览器中输入以下网址example.com/index.html#tab-1并按回车键 - 页面不会加载。

我想获取tab1参数并使用id tab1加载内容。

我的代码:

我调用这些函数:

//to load

showTabContent(gethash);


showTabContent: function(id)
        {
            if(id != null)
            {
                var tabid = id.split("-");
                $("#tab"+tabid[1]).show();
            }
        },

gethash: function()
        {
            var hash = window.location.hash;
            if($.trim(hash) != "") return hash;
            else return null;
        }
2个回答

0

看看这个 jQuery 插件:

JS文件:https://raw.github.com/cowboy/jquery-hashchange/master/jquery.ba-hashchange.min.js

您的代码解决方案:

HTML:

<a href="#tab-1">Tab1</a>
<div id="tab1"></div>

JS:

obj = {
    checkHash: function(){
        $(window).hashchange( function(){
            var hash = location.hash;
            obj.showTabContent(hash);
        })
    },
    showTabContent: function(id)
    {
        var tabid = id.split("-");
        $("#tab"+tabid[1]).show();
    } 
};

//init
obj.checkHash();

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接