jQuery is not finding elements

后端 未结 4 1146
伪装坚强ぢ
伪装坚强ぢ 2021-01-21 06:06

jQuery is not finding any elements. alert($(\"#testbutton\").length); displays 0 every time.

Am I doing something wrong?

My JS / jQuery cod

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-21 06:21

    If you write jQuery script in the head using:

    (function() {
    
        ...
    
    })();
    

    it doesn't work because it may execute the script before loading the content of the body page.

    Use:

    $(document).ready(function() {
    
        ...
    
    });
    

    or move your script at the footer.

提交回复
热议问题