jquery .ready() equivalent in d3js?

2020-07-03 09:27发布

问题:

currently i am adding all javascript at HTML <head> after uglying as file & using these jquery functions to check document ready

$( document ).ready(function() {}); OR $(function() {});

is there any d3js equivalent to remove using of these jquery functions ?

回答1:

A

You can simply put the <script> tag at the bottom of the body tag.

B

you can add the DOMContentLoaded event and insert your d3js code inside.

document.addEventListener("DOMContentLoaded", function(e) {
   /* Your D3.js here */
  });

Direct Answer

There is no equivalent to the jQuery function in the D3.js library, the snippet written above will work as well.



回答2:

There is no specific d3 way but,

if your javascript is below any of the html elements then the dom is loaded before it starts executing the javascript.

Putting your javascript at the bottom of the body is usually good enough. You can also use native js method which works in most of the browsers.

document.addEventListener("DOMContentLoaded", function(event) { 
  //do work
});