如何使用jQuery获得锚文本/href? [英] How to get anchor text/href on click using jQuery?

查看:143
本文介绍了如何使用jQuery获得锚文本/href?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一下,我有一个看起来像这样的锚点

Consider I have an anchor which looks like this

 <div class="res">
     <a href="~/Resumes/Resumes1271354404687.docx">
         ~/Resumes/Resumes1271354404687.docx
     </a>
 </div>

注意:锚点将没有任何ID或类...

NOTE: There won't be any id or class for the anchor...

我想在该锚的jQuery onclick中获取href/文本.

I want to get either href/text in the jQuery onclick of that anchor.

推荐答案

注意:将类info_link应用于您要从中获取信息的任何链接.

Note: Apply the class info_link to any link you want to get the info from.

<a class="info_link" href="~/Resumes/Resumes1271354404687.docx">
    ~/Resumes/Resumes1271354404687.docx
</a>

对于href:

$(function(){
  $('.info_link').click(function(){
    alert($(this).attr('href'));
    // or alert($(this).hash();
  });
});

对于文本:

$(function(){
  $('.info_link').click(function(){
    alert($(this).text());
  });
});

.

您现在可以像这样获得它们:

You can get them like this now:

对于href:

$(function(){
  $('div.res a').click(function(){
    alert($(this).attr('href'));
    // or alert($(this).hash();
  });
});

对于文本:

$(function(){
  $('div.res a').click(function(){
    alert($(this).text());
  });
});

这篇关于如何使用jQuery获得锚文本/href?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆