$(location).attr('href'); not working

后端 未结 6 1220
醉梦人生
醉梦人生 2021-01-28 23:22

I do not know why but I have problems with this code. The banner is displayed on every page although it has specified the attribute $(location).attr(\'href\') you can help me?:<

相关标签:
6条回答
  • 2021-01-28 23:34

    location is not a DOM Element, it is a Location object. You are trying to create a jQuery object from it, but it does not make sense.

    Use this instead:

    var currentUrl = window.location.href;
    
    0 讨论(0)
  • 2021-01-28 23:37

    ok, I made ​​the change in this way, but did not seem to work:

    $(document).ready(function() {
    var currentUrl = window.location.href;
    if(currentUrl == 'http://streamingdb.net/')  
    $('#bottombar').show(); 
    $("#bottombarClose").click(function() {
    $('#bottombar').hide();
    });
    });
    

    then I added a "display:none" to div#Bottombar and everything works. Thanks to all of your time.

    0 讨论(0)
  • 2021-01-28 23:38

    Instead of

    $(location).attr('href'); 
    

    use

    currentUrl = location;
    
    0 讨论(0)
  • 2021-01-28 23:39

    Don't you mean just location.href?

    This is assuming you are talking about window.location and not something else.

    Returns a Location object, which contains information about the URL of the document and provides methods for changing that URL. You can also assign to this property to load another URL.

    Location is not an element in the dom thus jQuery can not select it.

    So your code would be like so:

    var currentUrl = window.location.href;
    
    0 讨论(0)
  • 2021-01-28 23:39

    Try this

     var currentUrl = window.location.href;
    
    0 讨论(0)
  • 2021-01-28 23:42

    Try using var currentUrl = window.location.href instead. This will return the location of the current page.

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