开发者

Why click event not work when I click a image at runtime?

开发者 https://www.devze.com 2022-12-23 22:16 出处:网络
Now I am developin开发者_如何学Cg a Firefox extension , and when web page I want to create a image at runtime, now I can show the image , but When I click it to invoke a method , why it is not work? i

Now I am developin开发者_如何学Cg a Firefox extension , and when web page I want to create a image at runtime, now I can show the image , but When I click it to invoke a method , why it is not work? it is my code:

var _img = doc.createElement("img");

_img.setAttribute("id", "floatImage");

_img.setAttribute("src", "abc.jpg");

_img.setAttribute("onclick", "clickimage()");

document.body.append(_img);

function clickimage() { alert("click"); }


Instead of an "onclick" attribute try adding click event listener

_img.addEventListener('click', clickimage);
0

精彩评论

暂无评论...
验证码 换一张
取 消