ajax hash方法,jquery - Ajax hash in url - Stack Overflow

博客内容涉及到网页URL中hash值的问题,用户希望通过点击导航链接实现#contact替换#home,并在页面加载时直接跳转到特定的hash页面,如#contact。当前代码存在重复选择器和点击同一链接时页面重新加载的问题。作者使用了htaccess来隐藏文件扩展名,并提及了前端路由和页面内容的动态加载。
摘要由CSDN通过智能技术生成

I am using code from this tutorial

In the URL, it is weird; If I were to click on home, it'll go to example.tld/home#, but I would like to have the hash on the back. example.tld/#home. If I were to go to the contact page, it'll display as /home#co … doesn't display the whole word. (contact). I would like #contact to replace #home when I change the page. Also how can I land on a different page on load instead of #home again? Example: click on example.tld/#contact, goes to #contact page.

Also...Here is the code I have modified from the tutorial:

// Check for hash value in URL

var hash = window.location.hash.substr(1);

var href = $('nav ul li a').each(function(){

var href = $(this).attr('href');

if(hash==href.substr(0,href.length-5)){

var toLoad = hash+'.html #main-content';

$('#content').load(toLoad)

}

});

$('nav ul li a').click(function(){

var toLoad = $(this).attr('href')+' #main-content';

$('#main-content').hide('fast',loadContent);

$('#load').remove();

$('#wrapper').append('LOADING...');

$('#load').fadeIn('normal');

window.location.hash = $(this).attr('href').substr(0,$(this).attr('href').length-5);

function loadContent() {

$('#main-content').load(toLoad,'',showNewContent())

}

function showNewContent() {

$('#main-content').show('normal',hideLoader());

}

function hideLoader() {

$('#load').fadeOut('normal');

}

return false;

});

Think I should cach some of the selectors that are being repeated?

Also here is the HTML:

… content to be loaded

Nav:

I'm using htaccess so I do not have to include the file extension, but if its needed, the files are .php

Let's say i'm on #home and if I click the #home link again, it'll reload the same page, is there any way to stop this if i'm already on that page?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值