jQuery is not finding elements

后端 未结 4 1103
伪装坚强ぢ
伪装坚强ぢ 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-21 06:20

    When your code runs the DOM is not ready so the element doesn't exist. Did you mean to do this instead (passing a function to jQuery is a shortcut for $(document).ready(fn)):

    $(function () {
        alert($("#testbutton").length);
    });
    

提交回复
热议问题