Matching a link's fragment with the url's fragment with jquery?
-
29-10-2019 - |
سؤال
How can I find the match in a link's fragment with the url's fragment and then highlight the link's parent?
HTML,
<div class="item">
<a href="http://example.come/#/story/article-1/">Link 1</a>
</div>
<div class="item">
<a href="http://example.come/#/story/article-2/">Link 2</a>
</div>
<div class="item">
<a href="http://example.come/#/story/article-3/">Link 3</a>
</div>
<div class="item">
<a href="http://example.come/#/story/article-4/">Link 4</a>
</div>
jquery,
//var fragment = location.hash;
fragment = '#/story/article-3/';
string = $('.item').find('a').attr('href');
array_string = string.split('#');
last_item = array_string[array_string.length - 1];
if(fragment == '#'+last_item)
{
alert('match!');
// this is my theory... but I don't know how to get the object that matches the fragment.
match_object.parents().css({background:'red'});
}
So, in this case, the Link 3's container element should be highlighted.
لا يوجد حل صحيح
لا تنتمي إلى StackOverflow