jQuery - function runs fine when fired on div click, but not on page load

后端 未结 5 924
[愿得一人]
[愿得一人] 2021-01-25 17:49

I have the following function that I want to run on page load and re-run when you click div#trigger. When you click div#trigger everything is fine. However on page load the code

5条回答
  •  暖寄归人
    2021-01-25 18:26

    Wrap the function call in a document.ready handler like this...

    $(function() {
        textIndentFunc();
    });
    

    That makes sure that the DOM is loaded before trying to do anything to manipulate it, otherwise you'll get different results at different times, depending on what's loaded and what's not.

提交回复
热议问题