document.ready像javascript中的功能吗? [英] document.ready like functionality in javascript?

查看:56
本文介绍了document.ready像javascript中的功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,要求仅使用纯JavaScript而不使用jQuery.我需要在加载DOM之后而不是在页面完全加载时初始化一些变量(使用某些函数).简而言之,它不应该等待整个页面加载.在jQuery中,可以很容易地使用document.ready()函数来完成此操作.

I have a requirement that states to use only plain JavaScript and not jQuery. I need to initialize some variables(using some function) as soon as the DOM is loaded and not when the page has fully loaded. In short it should not wait for the whole page to load. In jQuery it can be very easily done using document.ready() function.

是否可以使用任何函数在JavaScript中实现它?

Is it possible to implement it in JavaScript using any function?

推荐答案

一种实用"的方法是在文档末尾放置一个script块(甚至不完全等同于domready)

a "practical" way is just placing a script block before the end of the document (even is not really equivalent to domready)

  ...
  <script>...</script>
  </body>
</html>

或使用DomReady事件的各种纯js实现之一,例如 http://snipplr.com /view/6029/domreadyjs/

or use one of various pure-js implementation of DomReady event, like http://snipplr.com/view/6029/domreadyjs/

这篇关于document.ready像javascript中的功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆