Script does not work when on <head>

后端 未结 3 1124
一向
一向 2020-12-11 09:37

I proved script. its works, but outside of . I am not good on script. maybe its a simple problem.




        
3条回答
  •  一生所求
    2020-12-11 09:55

    The problem is that the elements you are referencing, don't exist yet.

    To ensure they exist before using them, you have to put it's related code inside $(document).ready. So you have:

        $(document).ready(function(){ //This ensures DOM elements are loaded
          $('#img1').mouseover(function () {
            $('#p1').show("slow");
          });
    
          $("#p1").hover(function () {
            $("#p1").hide("slow");
          });
        });
    

    But, if you can't change that js file, to add a document.ready, you could load the script dynamically, as the following:

    
      ...
      
      ...
    
    

    It's not mandatory for the js scripts (in general) to be on the head section, but it's a good practice IMHO. However, other people prefer to put it at the bottom of the page for performance reasons. Hope this helps.

提交回复
热议问题