从url获取散列值 [英] Get hash value from url

查看:135
本文介绍了从url获取散列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在链接被点击后获取哈希值。任何想法?

I'm trying to get the hash value after a link was clicked. Any ideas?

例如

链接

The Link

index.html#needThis

这是我的结果:

index.htmlneedThis

如何删除'index.html'?

How can I remove the 'index.html' ?

$('#myselector').on('click', 'a', function(){
    var hash = $(this).attr('href').replace('#', '') //get url
    console.log(hash);
})


推荐答案

更新

现代浏览器(不知道它是怎么回事)可以提取散列直接来自锚元素(,所以我加了#5

Modern browsers (not sure how back it goes) can extract the hash directly from anchor elements (so i added #5)

请选择...


  1. var hash = $(this).attr('href')。split ('#')[1];

  2. var hash = $(this).attr('href')。replace /^.*?#/,'');

  3. var href = $(this).attr('href'),hash = href.substr(href.indexOf('#')+ 1);

  4. var hash = $(this).attr('href')。match(/#(。* $)/)[1];
  5. var hash = this.hash.substr(1);

  1. var hash = $(this).attr('href').split('#')[1];
  2. var hash = $(this).attr('href').replace(/^.*?#/,'');
  3. var href = $(this).attr('href'), hash = href.substr(href.indexOf('#')+1);
  4. var hash = $(this).attr('href').match(/#(.*$)/)[1];
  5. var hash = this.hash.substr(1);






update

刚刚重新审视了这一点,我相信#2可以改进 p>

Just revisited this and i believe that #2 can be improved to

$(this).attr('href').replace(/^.*?(#|$)/,'');

这样,如果不存在散列值,它将返回空值而不是整个网址。

This way if no hash exists it will return empty instead of the whole url..

这篇关于从url获取散列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆