我正在尝试在点击链接后获取散列值。有什么想法吗?
例如:
链接
index.html#needThis
这是我的结果:
index.htmlneedThis
如何删除'index.html‘?
$('#myselector').on('click', 'a', function(){
var hash = $(this).attr('href').replace('#', '') //get url
console.log(hash);
})
发布于 2012-11-12 21:05:16
只需使用var hash = window.location.hash
即可。
它返回#之后的所有内容
或者,如果你有一个包含url的变量:
$(this).attr('href').substring($(this).attr('href').indexOf('#'))
发布于 2012-11-12 21:08:05
您可以尝试使用窗口位置散列。
$('#myselector').on('click', 'a', function(){
var hash = $(this).attr('href').replace('#', '') //get url
var hash2 = window.location.hash ;
console.log(hash2);
})
发布于 2016-02-09 01:51:18
为什么不简单地使用:
window.location.hash
对我来说很好。
https://stackoverflow.com/questions/13344221
复制相似问题