2010-01-10 7 views
0

Est-il possible d'obtenir l'URI avec javascript ou est possible de briser le href du lien et si oui comment, j'essaye d'exécuter un ajax qui a des événements de survol et de clic et l'appel de méthode pour chaque ajax est le même, donc je dois pouvoir obtenir l'identifiant unique qui est passé dans l'URI.URI et aide jQuery

Répondre

0

HTML

<a href="https://stackoverflow.com/users/1/details">User</a> 

jQuery

$("a").click(function(e){ 
    var href = $(this).attr('href'); // = /users/1/details 

    //Once you have it, get the id: 
    var id = href.split('/')[2]; // returns 1 

    //Do something with it 

    e.preventDefault(); // Keep the original link from being followed 
}); 
1

Une ancre met à disposition les mêmes propriétés que l'on trouve dans window.location.

E.g.

<a id="mylink" href="http://website.com/page.html#content">Link</a> 

jQuery:

var anchor = $('a#mylink')[0]; // [0] to access DOM node 

anchor.href; // => http://website.com/page.html#content 
anchor.pathname; // => page.html 
anchor.hash; // => #content 
anchor.protocol; // => http: