Javascript ajax哈希导航无法正常工作
我想使用哈希值进行导航,但每次加载页面时,我的脚本都会将初始哈希值重置为#home,无论我在url中添加了什么哈希值: 下面是启动的脚本,用于测试哈希是否存在以及在#内容中加载什么:Javascript ajax哈希导航无法正常工作,javascript,jquery,ajax,hash,navigation,Javascript,Jquery,Ajax,Hash,Navigation,我想使用哈希值进行导航,但每次加载页面时,我的脚本都会将初始哈希值重置为#home,无论我在url中添加了什么哈希值: 下面是启动的脚本,用于测试哈希是否存在以及在#内容中加载什么: 有人能帮我一下吗,或者告诉我为什么这个脚本将#home设置为默认值。window.location.hash返回带有数字符号的哈希 这可以使用4种方法中的1种进行修复。如果不需要,我也会删除第一个 方法#1(最佳):从哈希属性中移除# 方法2(最有效):使用开关和substr散列 方法#3(最简单):添加散列 方法
有人能帮我一下吗,或者告诉我为什么这个脚本将#home设置为默认值。window.location.hash返回带有数字符号的哈希 这可以使用4种方法中的1种进行修复。如果不需要,我也会删除第一个
方法#1(最佳):从哈希属性中移除#
方法2(最有效):使用开关和substr
散列
方法#3(最简单):添加散列
方法4(最差):使用indexOf
这里
它返回的不是“家”,而是“家”。
将哈希替换为Substr,或与#一起使用。
对于调试,使用警报(window.location.hash)代码>我尝试了两种方法,但两种方法都不起作用。我能给你更多的信息吗?每当我键入url并添加#news并在url中按enter键时,它都会将所有内容还原并将哈希更改为#home,同时加载home contentI updated方法1。试试看。尝试发出警报散列
,并确保其值符合您的需要。@DJERock请看这里:。代码:我没有尝试方法2和方法3,但是方法1非常有效!!!谢谢你的帮助!不客气!我刚刚添加了其他方法,只是为了说明有很多方法可以解决这个问题。
$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();
}
$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();
}
$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;
}
$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();
}
$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();
}
window.location.hash == 'home'