jQuery从url hash中查找属性

Jef*_*rey 2 url hash jquery attributes find

尝试从url哈希中查找属性值.

// 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 to have a value of 3
Run Code Online (Sandbox Code Playgroud)

所有帮助赞赏.谢谢 :)

Nic*_*ver 7

您可以使用attribute-equals选择器执行此操作,如下所示:

$('a[href="'+window.location.hash+'"]').attr("rel")
Run Code Online (Sandbox Code Playgroud)

或者.filter(),像这样:

$("a").filter(function() { return this.hash == location.hash; }).attr("rel")
Run Code Online (Sandbox Code Playgroud)

.hash与启动#将是一致的,所以没有必要删除/添加此回,只是用它原样.