admin管理员组文章数量:1323681
I'm using this jQuery selector:
$("a[href*='#']").click(function(e) {
e.preventDefault();
alert(#valueinhere)
});
to select links that link to an anchor
My links are absolute and relative so they can look like or like
test#anchor
or like #anchor
How to get the string #anchor
from all links above? Is there any regex or something like that?
(I can't use window.location.hash
because of e.preventDefault()
that is necessary)
I'm using this jQuery selector:
$("a[href*='#']").click(function(e) {
e.preventDefault();
alert(#valueinhere)
});
to select links that link to an anchor
My links are absolute and relative so they can look like http://www.myweb./test#anchor
or like test#anchor
or like #anchor
How to get the string #anchor
from all links above? Is there any regex or something like that?
(I can't use window.location.hash
because of e.preventDefault()
that is necessary)
-
1
You want to get the
hash
from thehref
of thea
elements, or from the browser's address bar/location? – David Thomas Commented Dec 30, 2011 at 19:58 -
@David did you read the post? Clearly he wants the hash from the
a
tag – Eonasdan Commented Dec 30, 2011 at 20:01 -
So why is he even considering
window.location.hash
? And yes; I did read the question, before asking my own question. – David Thomas Commented Dec 30, 2011 at 20:05 -
I just added
window.location.hash
info because sometimes there are some people who reply in the way I don't need to – simPod Commented Dec 30, 2011 at 20:09
4 Answers
Reset to default 4Why can't you get the hash
from the anchor element?
this.hash
It is independent of whether you're preventing the default behavior of the click event.
$("a[href*='#']").click(function(e) {
e.preventDefault();
alert(this.hash);
});
Here's a working example.
$("a[href*='#']").click(function(e) {
e.preventDefault();
alert( this.href.split("#")[1] )
});
alert(this.href.split("#")[1]);
Make use of indexOf
in String
$('a[href*="#"]').click(function(e) {
var url = $(this).attr('href');
e.preventDefault();
var newUrl = url.substring(url.indexOf("#"));
alert(newUrl);
});
FIDDLE : http://jsfiddle/8SdW2/1/
本文标签: javascriptGet anchor value from absolute url stringStack Overflow
版权声明:本文标题:javascript - Get anchor value from absolute url string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742141764a2422609.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论