Jak uzyskać kotwicę z adresu URL za pomocą jQuery?
Mam taki adres URL:
www.example.com/task1/1.3.html#a_1
Jak mogę uzyskać wartość a_1
anchor używając jQuery i zapisać ją jako zmienną?
151
6 answers
Możesz użyć .indexOf()
oraz .substring()
, Tak:
var url = "www.aaa.com/task1/1.3.html#a_1";
var hash = url.substring(url.indexOf("#")+1);
Możesz spróbować tutaj, jeśli może nie mieć #
w nim, wykonaj if(url.indexOf("#") != -1)
sprawdzić Tak:
var url = "www.aaa.com/task1/1.3.html#a_1", idx = url.indexOf("#")
var hash = idx != -1 ? url.substring(idx+1) : "";
Jeśli jest to bieżący adres URL strony, możesz po prostu użyć window.location.hash
, aby go pobrać i zastąpić #
, Jeśli chcesz.
183
Author: Nick Craver,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-24 01:26:50
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-24 01:26:50
Możesz użyć tego:
var hash = window.location.hash.substr(1);
434
Author: Silvio Delgado,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-26 19:00:01
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-26 19:00:01
Użyj
window.location.hash
Aby odzyskać wszystko poza i w tym #
55
Author: Real,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-05 22:39:18
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-08-05 22:39:18
JQuery style:
$(location).attr('hash');
31
Author: Valentin E,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-01 10:11:34
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-01 10:11:34
Możesz użyć poniższej "sztuczki", aby przeanalizować dowolny poprawny adres URL. Wykorzystuje on specjalną właściwość href anchor element, hash
.
Z jQuery
function getHashFromUrl(url){
return $("<a />").attr("href", url)[0].hash.replace(/^#/, "");
}
getHashFromUrl("www.example.com/task1/1.3.html#a_1"); // a_1
Z prostym 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
7
Author: David Murdoch,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-14 17:58:19
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-14 17:58:19
Jeśli masz tylko zwykły ciąg url (i dlatego nie masz atrybutu hash), możesz również użyć wyrażenia regularnego:
var url = "www.example.com/task1/1.3.html#a_1"
var anchor = url.match(/#(.*)/)[1]
2
Author: DrewB,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-25 18:08:38
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-25 18:08:38