9
var del = document.createElement('input');
del.type = 'button';
del.name = 'delll';
del.value = 'del';
del.onClick = 'alert("hi  javascript")';

Here I have dynamically created a input type BUTTON and now I want to call function on button click event. I am using onClick(); function for this. In the above code all is working fine but del.onclick is not working as I want (for generating alert for demo)

I am not using any jquery code in this program so please don't use any jquery code.

4

4 回答 4

7

将 onclick(全部小写)设置为匿名函数

del.onclick = function(){ alert('hi javascript');};

请注意,该函数不像其他属性那样用引号括起来

于 2013-09-16T06:36:32.703 回答
3
del.onclick = function () {
    alert("hi  jaavscript");
};

使用 small "C"inonClick并为其传递一个函数。

演示在这里

于 2013-09-16T06:38:20.617 回答
1

像这样试试

    var del = document.createElement('input');
    del.setAttribute('type', 'button');
    del.setAttribute('name', 'delll');
    del.setAttribute('value', 'del');
    del.setAttribute('onClick', 'alert("hi  jaavscript")');
于 2013-09-16T06:37:14.087 回答
0

所以要详细回答这个问题:

del.onclick = '' 不会在引号内“执行”某些内容。如果要执行/调用函数,则需要将该函数作为参数传递。

IE

del.onclick = function() { alert('hi there!')}
于 2013-09-16T06:37:02.840 回答