One thing that must not be forgotten at the moment in js or jquery

1. As a novice, when I was learning cavas, there was a relatively common novice problem;

Look at the code below

                        var mycanvas = document.getElementById("mycanvas");
var cxt = mycanvas.getContext("2d");
cxt.moveTo(10, 10);
cxt.lineTo(150, 50);
cxt.lineTo(10, 50);

cxt.stroke();

Do you think there is no problem?

Indeed, there is really no problem from the code point of view, but after running my cavas object is always null;

You probably know why: 

It should wait until the page is loaded, which means less window.onload

window.onload = function() {
var mycanvas = document.getElementById("mycanvas");
var cxt = mycanvas.getContext("2d");
cxt.moveTo(10, 10);
cxt.lineTo(150, 50);
cxt.lineTo(10, 50);
cxt.stroke();

}

There is no problem in writing like this;

If using jquery then yes, $(function(){

}) or $(docment).ready(...);

The problem is not big, but it is often easier to forget; as a novice, remember to remember!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325718162&siteId=291194637