how to call onclick function on dynamically created button in javascript

前端 未结 4 1066
花落未央
花落未央 2021-02-14 21:45
var del = document.createElement(\'input\');
del.type = \'button\';
del.name = \'delll\';
del.value = \'del\';
del.onClick = \'alert(\"hi  javascript\")\';
4条回答
  •  南笙
    南笙 (楼主)
    2021-02-14 22:26

    So to answer the question in details:

    del.onclick = '' does not "execute" something within quotes. If you want to execute/call a function, you would want to pass the function as a parameter.

    i.e

    del.onclick = function() { alert('hi there!')}
    

提交回复
热议问题