开发者

Adding onclick handler using jQuery attr method does not work in IE

开发者 https://www.devze.com 2023-01-11 16:05 出处:网络
Following code works fine in Firefox. In IE(8.0) button \"2\" does not work. $(\'<button type=\"button\" onclick=\"alert(1)\">1</button>\').appendTo($(\'body\'));

Following code works fine in Firefox. In IE(8.0) button "2" does not work.

$('<button type="button" onclick="alert(1)">1</button>').appendTo($('body'));
$('<button type="button" >2</button>').attr('onclick',开发者_运维技巧'alert(2)').appendTo($('body'));

Question: what am I doing wrong?


You should attach click handlers with .click() instead, like this:

$('<button type="button">2</button>').click(function() { alert(2); }).appendTo($('body'));

This is a shortcut for .bind('click', func), though which you can bind any event, several such shortcuts are available.

0

精彩评论

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