让我们假设我有这样的:获得具有相等的属性的元素的属性变量
<a href="/index" data-title="Home">LINK</a>
<a href="/page2" data-title="Another Page">LINK</a>
,我不得不从a
标签获得data-title
具有href
等于到location.pathname
。
我该如何使用jQuery来做到这一点?
让我们假设我有这样的:获得具有相等的属性的元素的属性变量
<a href="/index" data-title="Home">LINK</a>
<a href="/page2" data-title="Another Page">LINK</a>
,我不得不从a
标签获得data-title
具有href
等于到location.pathname
。
我该如何使用jQuery来做到这一点?
使用下面的代码:更换,/index
表明片断
$('a[href="' + location.href + '"]').attr('data-title')
请参见下面的工作代码示例,但location.href
效果很好:
alert($('a[href="' + '/index' + '"]').attr('data-title'))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="/index" data-title="Home">LINK</a>
<a href="/page2" data-title="Another Page">LINK</a>
谢谢你,像一个魅力工作。 – Igor
@Igor欢迎您),祝您有美好的一天! –
我没试过任何东西,@DavidThomas。我不知道该怎么做。 – Igor
你是什么意思@句子“你不知道如何用CSS突出显示某些东西,如果你不知道该说什么,请不要说”? – Prasad
让我们[在聊天中继续讨论](http://chat.stackoverflow.com/rooms/105473/discussion-between-igor-and-n-v-prasad)。 – Igor