JQuery how to find all links on current page?

0 like 0 dislike
52 views
Objective: to find all links in a given div, which point to the current page.


Wrote here such a selector

$('div#top_menu a[href="' + document.URL + '"]')



but he finds nothing, because the links in the div relative (/>),

and document.The URL is an absolute link (localhost/account/page.aspx).


This query produces the required absolute link:

$('div#top_menu a[href="page.aspx"]')[0].href



but I like this property to make a request?
by | 52 views

6 Answers

0 like 0 dislike
Filter through each:
$('div#top_menu a').each(function(){
if (this.href='xxxxxxxx') { %do_something% }
});
by
0 like 0 dislike
Thank you all, I'm more just the decision of abrowser , here's a reworking of his decisions came to me:
\r
\r
$(document).ready(function () { $('div#top_menu a').each(function(){ if (this.href == document.URL) { $(this).addClass('b-menu_current'); } }); }); 

\r
PS: jquery 1.4.1
by
0 like 0 dislike
Perhaps you need to take the last piece of the url just?
\r
$('div#top_menu a[href="' + document.URL.substring(document.URL.lastIndexOf("/")+1,document.URL.length) + '"]') 

\r
But in your case (c [0]) you take a js object and not jquery and see he has a href property (for which you made a selection)
by
0 like 0 dislike
$(function(){ var path = location.pathname.substring(1); if ( path ) $('div#topmenu a[href$="' + path + '"]').attr('class', 'selected'); }); 
by
0 like 0 dislike
document.location.pathname to help you
by
0 like 0 dislike
You need to solve in reverse order. Ie to take all referring to within the specified div'a and to filter the href so that the current reference is to the end of the document.URL, even here the jQuery is not needed
\r
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]);//the result as a jquery object } 

\r
But there is a small "but", e.g. "mysite/" and "mysite/index.html" point to the same page, with all the attendant nuances
by
110,608 questions
257,186 answers
0 comments
32,865 users