JavaScript 中 $(document).ready() 的等价函数是什么?

javascriptweb developmentfront end technology

在 jQuery 中,如果希望事件在页面上起作用,则应在 $(document).ready() 函数内调用该函数。该函数内的所有内容将在 DOM 加载后、页面内容加载前加载。

$(document).ready(function() {
   alert(“Document loaded successful!");
});

示例

在 JavaScript 中,若要实现与 $(document).ready 相同的结果,请尝试以下代码 −

<html>
   <body>
      <script>
         var loader = setInterval(function () {
            if(document.readyState !== "complete") return;
            clearInterval(loader);
            alert(“Document loaded successful!");
            // document.write("Document loaded successful!");
         }, 300);
      </script>
   </body>
</html>

相关文章


有用资源