如何使用jquery获取链接的href? [英] How to get the href of a link using jquery?
本文介绍了如何使用jquery获取链接的href?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的jquery代码:
I have a simple jquery code:
$(function(){
$('#note_pag a').click(function(){
alert('click');
return false;
});
});
这是html代码:
<div id="note_pag">
<ul>
<li><a href="1">1</a></li>
<li><a href="2">2</a></li>
<li><a href="3">3</a></li>
<li><a href="4">4</a></li>
</ul>
</div>
我的问题是:如何获得用户点击的链接的href?
my question is: how can i get the href of the link clicked from the user?
谢谢
推荐答案
在事件处理程序中,this
是对所单击元素的引用.然后,您可以使用该DOM元素构建jQuery选择,然后使用 attr
函数获取href
属性的值:
Within the event handler, this
is a reference to the element clicked. You can then use that DOM element to build a jQuery selection, and then use the attr
function to get the value of the href
attribute:
$(function(){
$('#note_pag a').click(function(){
alert('cliccato link');
alert($(this).attr('href'));
return false;
});
});
这篇关于如何使用jquery获取链接的href?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文