Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.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
如何将URL中的哈希链接链接到Javascript内容?_Javascript - Fatal编程技术网

如何将URL中的哈希链接链接到Javascript内容?

如何将URL中的哈希链接链接到Javascript内容?,javascript,Javascript,我曾经使用jQueryUI将 一切都很顺利,我注意到每个标签都绑定了一个URL哈希标签(我不知道你是用英语说的)。例如,单击第一个选项卡后,#Tab0被添加到我的URL中 我想在我当前的项目中重现这种行为。但是我没有使用jQueryUI选项卡,我正在移植一个桌面应用程序,还有JavaScript按钮,可以在我的页面中写入和替换内容(以不同页面的方式,但不需要重新加载) 我如何继续模仿这种行为?我是否必须手动获取URL中的标记并通过JavaScript执行相应的操作 谢谢,你可以这样做: 使用当前

我曾经使用jQueryUI将

一切都很顺利,我注意到每个标签都绑定了一个URL哈希标签(我不知道你是用英语说的)。例如,单击第一个选项卡后,
#Tab0
被添加到我的URL中

我想在我当前的项目中重现这种行为。但是我没有使用jQueryUI选项卡,我正在移植一个桌面应用程序,还有JavaScript按钮,可以在我的页面中写入和替换内容(以不同页面的方式,但不需要重新加载)

我如何继续模仿这种行为?我是否必须手动获取URL中的标记并通过JavaScript执行相应的操作


谢谢,

你可以这样做:

使用当前url的哈希值创建url:

var url = window.location.href + '#Tab0';
var hash;

if (window.location.href.indexOf('#') > -1)
{
    hash = url.split('#')[1];
}
正在从当前url读取哈希:

var url = window.location.href + '#Tab0';
var hash;

if (window.location.href.indexOf('#') > -1)
{
    hash = url.split('#')[1];
}

您可以使用
location.hash

//you can set hash
location.hash = "something"; 

//and read it eg. http://domain.com#something
console.log(location.hash); //will return the current hash value 'something'

另外,您必须记住,如果您的锚定标记具有哈希href属性,例如
,它将自动附加到当前url,浏览器将尝试在页面上查找给定的id。当然,您可以防止这种默认行为。

因此,基本上一切都是手工完成的。如果我没有其他任何东西,我会将此标记为已回答,谢谢。您也可以在这里查看: