如何在JavaScript中动态创建的按钮上调用onclick函数 [英] how to call onclick function on dynamically created button in javascript

查看:64
本文介绍了如何在JavaScript中动态创建的按钮上调用onclick函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

在这里,我已经动态创建了一个输入类型按钮,现在我想在按钮单击事件上调用函数。我为此使用 onClick(); 函数。在上面的代码中,一切正常,但是 del.onclick 不能正常工作(为演示生成警报)

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)

我在此程序中未使用任何jquery代码,因此请不要使用任何jquery代码。

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

推荐答案

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

set the onclick (all lower case) to an anonymous function

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

请注意,该函数不在其他属性的引号内

note the function is not in quotes like other attributes

这篇关于如何在JavaScript中动态创建的按钮上调用onclick函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆