How to get anchor text/href on click using jQuery?

前端 未结 5 1599
攒了一身酷
攒了一身酷 2020-11-29 20:25

Consider I have an anchor which looks like this

 
      
5条回答
  •  北荒
    北荒 (楼主)
    2020-11-29 20:45

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

    
        ~/Resumes/Resumes1271354404687.docx
    
    

    For href:

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

    For Text:

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

    .

    Update Based On Question Edit

    You can get them like this now:

    For href:

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

    For Text:

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

提交回复
热议问题