jquery source's position in html file is important or not

后端 未结 5 945
再見小時候
再見小時候 2021-01-21 16:38

This is an example of click button to pop up an alert.



        
5条回答
  •  走了就别回头了
    2021-01-21 16:52

    Scripts are executed in order they are encountered in the DOM1

    So yes, order between scripts matters.

    In this case, your script runs before jQuery does, so attempting to access $ results in a ReferenceError. To "fix" it, put your script under the jQuery script element. It doesn't matter if the scripts are in or at the bottom of the : but the relative order of the scripts is important.

    Here is a trivial mockup of the invalid ordering:

    
    
    

    If these the order of two script elements were reversed then "foo" would be alerted, as expected.


    1 (Except, perhaps, for those marked as async=true, which I will not talk about.)

提交回复
热议问题