Cannot set property 'src' of null

前端 未结 4 2021
一整个雨季
一整个雨季 2021-01-21 12:25

I am trying to rotate an image using javascript and I was trying some code I found on adobes website that does what I was looking for. When I run it it gives me the error: Unca

4条回答
  •  一生所求
    2021-01-21 12:38

    When in the head (look at the console):

    http://jsfiddle.net/m2wce/

    This happens because you are declaring the closure and executing right away. When you execute it in the head, the DOM is not ready (in other words, the HTML below still hasn't been "mounted" by the browser, as it processes the page sequencially, from top to botto) and at the time of the execution the img tag does not yet exist (because it is down below in the HTML page).

    When in the body, after the referenced img, it works:

    http://jsfiddle.net/m2wce/1/

提交回复
热议问题