JS怎么在onload方法外部取到里面的值

现在这么写是错的,浏览器会报错,可惜不知道错在哪,在下新手,请路过大神指教一下

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>导航</title>
<style type="text/css">
<script>
function drawOnCanvas ()
    {
    var x = 1;
    var img = new Image;
    var callback = function ()
    {
    img.src = "pic.jpg";
    x = 2;
    return x;
    }
    img.onload = callback;
    img.onload ();
    console.log (x);
    document.body.appendChild (img);
    }
    
    window.onload = drawOnCanvas;
</script>
</head>
<body>
</body>
</html>

追问

十分感谢回答,可是还是取不到里面的值,只要原因是由于得不到e.target呀

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答