Answer the question
In order to leave comments, you need to log in
jQuery how to find all links on current page?
Goal: Find all links in the given div that point to the current page.
I wrote this selector
$('div#top_menu a[href="' + document.URL + '"]')
$('div#top_menu a[href="page.aspx"]')[0].href
Answer the question
In order to leave comments, you need to log in
Filter through each:
$('div#top_menu a').each(function(){
if (this.href='xxxxxxxx') { %do_something% }
});
Thank you all very much, most of all I need the solution of the habrauser
$(document).ready(function () {
$('div#top_menu a').each(function(){
if (this.href == document.URL) {
$(this).addClass('b-menu_current');
}
});
});
PS: jquery 1.4.1
Perhaps you just need to take the last piece of the url?
$('div#top_menu a[href="' + document.URL.substring(document.URL.lastIndexOf("/")+1,document.URL.length) + '"]')
$(function(){
var path = location.pathname.substring(1);
if ( path )
$('div#topmenu a[href$="' + path + '"]').attr('class', 'selected');
});
Solve in reverse order. Those. take all the links inside the given div and filter them by href so that the current link is the ending for document.URL, even jQuery is not needed here
var links = document.getElementById('myDiv').getElementsByTagName('a'),
result = [], $result;
for(var i = links.length - 1, j = 0; i > -1; i--) {
if( document.URL.indexOf(links[i].href) + links[i].href.length != document.URL.length ) { continue; }
result[j++] = links[i];
$result = $result.jquery ? $result.add(links[i]) : $(links[i]);//результат как jquery объект
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question