Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jquery从url中获取链接锚点_Jquery - Fatal编程技术网

使用jquery从url中获取链接锚点

使用jquery从url中获取链接锚点,jquery,Jquery,我有一个像这样的链接 <a href="example.com#exampleId>Link</a> 我需要将值“#exampleId”输入到变量中。我如何才能做到这一点?我刚刚找到了没有#的解决方案 感谢您的帮助和问候试试这个: $("a").click( function(){ console.log(this.hash); // => #exampleId }); 请在此处查看:这是在对象中获取锚定链接部分的好技巧,请尝试此代码 <a

我有一个像这样的链接

<a href="example.com#exampleId>Link</a>

我需要将值“#exampleId”输入到变量中。我如何才能做到这一点?我刚刚找到了没有#的解决方案

感谢您的帮助和问候

试试这个:

$("a").click( function(){
    console.log(this.hash);  // => #exampleId
});

请在此处查看:

这是在对象中获取锚定链接部分的好技巧,请尝试此代码

<a href="example.com#exampleId">Link</a>

<script>
var parse  = document.createElement('a');
parse.href = document.getElementsByTagName("a")[0];
alert(parse.hash);
</script>

var parse=document.createElement('a');
parse.href=document.getElementsByTagName(“a”)[0];
警报(parse.hash);

或者您可以使用锚元素:

var url = document.createElement('a');
url.href = $('a').attr('href');
console.log(url.hash);

你什么时候需要在事件中获取页面加载?单击事件。在页面加载时,我可以使用window.location.hash进行加载
var url = document.createElement('a');
url.href = $('a').attr('href');
console.log(url.hash);