2010-01-10 52 views
0

是否有可能獲得與JavaScript的URI或可能分裂的鏈接的HREF,如果是這樣,我試圖運行一些AJAX懸停並單擊事件和方法調用每個ajax是相同的,所以我需要能夠獲得在URI中傳遞的唯一ID。URI和jQuery幫助

回答

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

錨可以使您在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: