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

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

Consider I have an anchor which looks like this

 
                      
相关标签:
5条回答
  • 2020-11-29 20:30

    Without jQuery:

    You don't need jQuery when it is so simple to do this using pure JavaScript. Here are two options:

    • Method 1 - Retrieve the exact value of the href attribute:

      Select the element and then use the .getAttribute() method.

      This method does not return the full URL, instead it retrieves the exact value of the href attribute.

      var anchor = document.querySelector('a'),
          url = anchor.getAttribute('href');
      
      alert(url);
      <a href="/relative/path.html"></a>


    • Method 2 - Retrieve the full URL path:

      Select the element and then simply access the href property.

      This method returns the full URL path.

      In this case: http://stacksnippets.net/relative/path.html.

      var anchor = document.querySelector('a'),
          url = anchor.href;
      
      alert(url);
      <a href="/relative/path.html"></a>


    As your title implies, you want to get the href value on click. Simply select an element, add a click event listener and then return the href value using either of the aforementioned methods.

    var anchor = document.querySelector('a'),
        button = document.getElementById('getURL'),
        url = anchor.href;
    
    button.addEventListener('click', function (e) {
      alert(url);
    });
    <button id="getURL">Click me!</button>
    <a href="/relative/path.html"></a>

    0 讨论(0)
  • 2020-11-29 20:42

    Edited to reflect update to question

    $(document).ready(function() {
        $(".res a").click(function() {
            alert($(this).attr("href"));
        });
    });
    
    0 讨论(0)
  • 2020-11-29 20:44

    Updated code

    $('a','div.res').click(function(){
      var currentAnchor = $(this);
      alert(currentAnchor.text());
      alert(currentAnchor.attr('href'));
    });
    
    0 讨论(0)
  • 2020-11-29 20:45

    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>
    

    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());
      });
    });
    
    0 讨论(0)
  • 2020-11-29 20:53

    Alternative

    Using the example from Sarfraz above.

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

    For href:

    $(function(){
      $('.res').on('click', '.info_link', function(){
        alert($(this)[0].href);
      });
    });
    
    0 讨论(0)
提交回复
热议问题