当我编写url并按Enter时,如何从url获取哈希码? [英] How can i get hash code from url when i write url and press enter?
本文介绍了当我编写url并按Enter时,如何从url获取哈希码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个网址:example.com/index.html#tab-1
I have this url: example.com/index.html#tab-1
我按照网址 example.com/index.html#tab-1 写入浏览器,然后按Enter键-页面将不会加载.
I write into browser following url example.com/index.html#tab-1 and press enter - page will not load.
我想获取参数tab1并加载ID为tab1的内容.
I want to get the parameter tab1 and load content with 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;
}
推荐答案
检查此jQuery插件:
Check this jQuery plugin:
- http://benalman.com/projects/jquery-hashchange-plugin/
- http://benalman. com/code/projects/jquery-hashchange/docs/files/jquery-ba-hashchange-js.html
- http://benalman.com/projects/jquery-hashchange-plugin/
- http://benalman.com/code/projects/jquery-hashchange/docs/files/jquery-ba-hashchange-js.html
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();
这篇关于当我编写url并按Enter时,如何从url获取哈希码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文