d3.js 和 document.onReady [英] d3.js and document.onReady

查看:32
本文介绍了d3.js 和 document.onReady的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用 d3.js 并且有一个细节完全没有我:如何仅在 DOM 准备好接收输入后才执行我的代码?

I'm just getting started with d3.js and there is one detail which is completely eluding me: how do I have my code execute only after the DOM is ready to receive input?

当然,我可以使用 jQuery 之类的东西,但这似乎过度了.

I could, of course, use something like jQuery but that seems excessive.

在我遇到的 每个 d3.js 示例中,似乎都有没有特殊的 document.onReady() 类型的例程,但所有示例都可以完美运行.但是,在我这边测试代码时,如果在 DOM 准备好之前执行代码,则代码完全失败(将我的代码放入 window.onload 确认了这一点).

In every d3.js example I've encountered there seems to be no special document.onReady() type of routine, yet all the examples work flawlessly. When testing code on my end, however, the code totally fails if executed before the DOM is ready (throwing my code into a window.onload confirms this).

是什么?

推荐答案

您会在他们的示例中注意到,他们的 javascript 位于任何被利用的 html 元素下方,以便在开始执行之前加载 dom 的一部分

You'll notice in their examples that their javascript is below any of the html elements that is utilized so that part of the dom is loaded before it starts executing the javascript.

只需将您的 javascript 放在正文的底部通常就足够了.

Simply putting your javascript at the bottom of the body is usually good enough.

这篇关于d3.js 和 document.onReady的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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