onmouseover doesn't work when using javascript to add img tag on IE

前端 未结 4 681
闹比i
闹比i 2021-01-13 14:24

I need some javascript code that dynamically adds an img tag to a div, and the img tag needs onmouseover and onmouseout handlers.

I have it working on Firefox. But

4条回答
  •  温柔的废话
    2021-01-13 15:07

    Ob's answer shows the correct method for attaching event listeners without using a JavaScript library (and was first), but I would like to add the following:

    onmouseover/out should never be attached as attributes. Basically, you're gone through all the trouble of not doing

    just to do it in your JavaScript. Event handlering callbacks should always be attached as event listeners via attachEvent for IE, and addEventListener for pretty much everyone else.

提交回复
热议问题