jQuery find attribute from url hash
Trying to find the attribute value from a url hash.
// URL
http://url.com/index.html#link
// HTML
<a href="#link" rel="3">Some link</a>
// JS
var anchor = window.location.hash.substring(1);
// Need help finding attribute value from finding the anchor
var attribute = .find('#+anchor').attr('rel'); //this needs t开发者_StackOverflowo have a value of 3
All help appreciated. Thanks :)
You can do it using an attribute-equals selector, like this:
$('a[href="'+window.location.hash+'"]').attr("rel")
Or .filter()
, like this:
$("a").filter(function() { return this.hash == location.hash; }).attr("rel")
The .hash
starting with #
will be consistent, so no need to remove/add this back, just use it as-is.
$('a[href=' + window.location.hash +']').attr("rel");
Is this what you need?
var attribute = $("a[href='#" + anchor + "']").attr('rel');
精彩评论