I have a URL that is like:
我的URL是:
www.example.com/task1/1.3.html#a_1
How can I get the a_1
anchor value using jQuery and store it as a variable?
如何使用jQuery获取a_1锚值并将其存储为变量?
6 个解决方案
#1
177
You can use the .indexOf()
and .substring()
, like this:
可以使用.indexOf()和.substring(),如下所示:
var url = "www.aaa.com/task1/1.3.html#a_1";
var hash = url.substring(url.indexOf("#")+1);
You can give it a try here, if it may not have a #
in it, do an if(url.indexOf("#") != -1)
check like this:
你可以在这里尝试一下,如果里面没有#,可以试试if(url.indexOf("#") != -1)
var url = "www.aaa.com/task1/1.3.html#a_1", idx = url.indexOf("#")
var hash = idx != -1 ? url.substring(idx+1) : "";
If this is the current page URL, you can just use window.location.hash
to get it, and replace the #
if you wish.
如果这是当前的页面URL,您可以使用windows .location。如果您愿意,可以使用散列来获取它,并替换#。
#2
422
You can use this:
您可以使用:
var hash = window.location.hash.substr(1);
#3
50
Use
使用
window.location.hash
to retrieve everything beyond and including the #
检索所有超出并包括#的内容
#4
29
jQuery style:
jQuery风格:
$(location).attr('hash');
#5
7
You can use the following "trick" to parse any valid URL. It takes advantage of the anchor element's special href-related property, hash
.
您可以使用以下“技巧”来解析任何有效的URL。它利用了锚元素的特殊href相关属性hash。
With jQuery
function getHashFromUrl(url){
return $("<a />").attr("href", url)[0].hash.replace(/^#/, "");
}
getHashFromUrl("www.example.com/task1/1.3.html#a_1"); // a_1
With plain JS
function getHashFromUrl(url){
var a = document.createElement("a");
a.href = url;
return a.hash.replace(/^#/, "");
};
getHashFromUrl("www.example.com/task1/1.3.html#a_1"); // a_1
#6
2
If you just have a plain url string (and therefore don't have a hash attribute) you can also use a regular expression:
如果你只有一个普通的url字符串(因此没有哈希属性),你也可以使用一个正则表达式:
var url = "www.example.com/task1/1.3.html#a_1"
var anchor = url.match(/#(.*)/)[1]
#1
177
You can use the .indexOf()
and .substring()
, like this:
可以使用.indexOf()和.substring(),如下所示:
var url = "www.aaa.com/task1/1.3.html#a_1";
var hash = url.substring(url.indexOf("#")+1);
You can give it a try here, if it may not have a #
in it, do an if(url.indexOf("#") != -1)
check like this:
你可以在这里尝试一下,如果里面没有#,可以试试if(url.indexOf("#") != -1)
var url = "www.aaa.com/task1/1.3.html#a_1", idx = url.indexOf("#")
var hash = idx != -1 ? url.substring(idx+1) : "";
If this is the current page URL, you can just use window.location.hash
to get it, and replace the #
if you wish.
如果这是当前的页面URL,您可以使用windows .location。如果您愿意,可以使用散列来获取它,并替换#。
#2
422
You can use this:
您可以使用:
var hash = window.location.hash.substr(1);
#3
50
Use
使用
window.location.hash
to retrieve everything beyond and including the #
检索所有超出并包括#的内容
#4
29
jQuery style:
jQuery风格:
$(location).attr('hash');
#5
7
You can use the following "trick" to parse any valid URL. It takes advantage of the anchor element's special href-related property, hash
.
您可以使用以下“技巧”来解析任何有效的URL。它利用了锚元素的特殊href相关属性hash。
With jQuery
function getHashFromUrl(url){
return $("<a />").attr("href", url)[0].hash.replace(/^#/, "");
}
getHashFromUrl("www.example.com/task1/1.3.html#a_1"); // a_1
With plain JS
function getHashFromUrl(url){
var a = document.createElement("a");
a.href = url;
return a.hash.replace(/^#/, "");
};
getHashFromUrl("www.example.com/task1/1.3.html#a_1"); // a_1
#6
2
If you just have a plain url string (and therefore don't have a hash attribute) you can also use a regular expression:
如果你只有一个普通的url字符串(因此没有哈希属性),你也可以使用一个正则表达式:
var url = "www.example.com/task1/1.3.html#a_1"
var anchor = url.match(/#(.*)/)[1]