jQuery this.href String Comparison Not Working

后端 未结 4 1269
予麋鹿
予麋鹿 2021-01-25 13:58

I have a simple jQuery script that I\'m trying to build upon but I can\'t get the href string comparison to return true:



        
相关标签:
4条回答
  • 2021-01-25 14:21

    Change:

    if ($(this).href
    

    To:

    if (this.href
    

    Or $(this).attr('href') but former is better.

    To read attributes, you need to use attr (shorthand for attribute)

    This is what you should have:

    if (this.href == "/Services/Cloud-Hosting") {
        alert('hi');
    }
    else {
        alert('no');
    }
    
    0 讨论(0)
  • 2021-01-25 14:28

    jQuery objects don't have an href property. Just access the property of the HTMLAnchorElement using this.href instead of creating a new jQuery object with $(this).

    0 讨论(0)
  • 2021-01-25 14:42

    try this:

    if ($(this).attr('href') == "/Services/Cloud-Hosting") {
    
    0 讨论(0)
  • 2021-01-25 14:43

    See .attr() and try this:

    $(this).attr('href') == "/Services/Cloud-Hosting"
    

    instead

    0 讨论(0)
提交回复
热议问题