2010-01-10 137 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: