this.href vs $(this).attr('href')

前端 未结 3 1065
醉酒成梦
醉酒成梦 2020-11-27 17:39

After reading this article net.tutsplus.com/tutorials/javascript-ajax/14-helpful-jquery-tricks-notes-and-best-practices/ I came to conclusion that using this.href<

相关标签:
3条回答
  • 2020-11-27 18:36

    document.getElementById(yourAnchorId).href.split("/")

    [document.getElementById(yourAnchorId).href.split("/").length - 1].split("?")[0];

    0 讨论(0)
  • 2020-11-27 18:38

    what a but a little substring?

    for example :

    function getHref(a)
    {
        var i = a.outerHTML.indexOf('href="')+6;
        return a.outerHTML.substring(i, a.outerHTML.indexOf('"', i));
    
    }
    

    P.S TESTED!

    0 讨论(0)
  • 2020-11-27 18:39

    The href property in plain Javascript will have the semantic attached to it. It returns the destination URL which the link will lead to. It doesn't matter how it was written (absolute or relative URLs).

    When you use the $(this).attr("href") you are retrieving directly the value of href attribute just like any other attribute, so it will return the exact value rendered in the HTML.

    For your case then, it's better to use $(this).attr("href")

    If you don't want to use jQuery, there's yet another solution, using just plain JavaScript:

    this.getAttribute('href')
    
    0 讨论(0)
提交回复
热议问题