无法将 onclick 属性添加到可行的输入
Cannot add onclick property to input viable
容易点。神秘问题。下面是我的代码。我为我的输入按钮创建了一个变量。我添加了一个点击功能。我将输入变量添加到现有的div。但是不会显示 onclick 属性。请帮忙。
var newbutton = document.createElement('input');
var addonclick = "'return sayhi('phrase')'";
newbutton.onclick = addonclick;//does not work
newbutton.type = 'button';
document.getElementById('existingdiv').appendChild(newbutton);
。
function sayhi(phrase){
alert(phrase);
}
也不起作用:
newbutton.id = "buttonid";
....
document.getElementById("buttonid").onload = '"return sayhi('phrase')"';
尝试添加newbutton.onclick = sayhi;
它将起作用。 OnClick 需要一个 JavaScript,该脚本将在单击该元素时执行。在这里addonclick
只是一个变量,单击该元素时没有任何要执行的内容。
将 sayhi 分配给 .onclick。还要给你的按钮一些文字
var newbutton = document.createElement('input');
newbutton.onclick = sayhi;
newbutton.type = 'button';
newbutton.value = 'click';
document.getElementById('existingdiv').appendChild(newbutton);
function sayhi(){
alert("hi");
}
onclick 属性接受函数而不是字符串,例如
newbutton.onclick = function(){sayhi('phrase')};
嘿,
你试过这种方式吗:
function sayhi(){
alert("hi");
}
var newbutton = document.createElement('input');
newbutton.onclick = sayhi;
newbutton.type = 'button';
document.getElementById('existingdiv').appendChild(newbutton);
相关文章:
- 为具有相同类的多个输入添加、移除和添加一个类
- 为收音机点击的输入添加值,然后删除
- 单击向输入添加文本值的链接
- 为数字输入添加特殊字符
- 敲除-将用户输入添加到表中的问题
- 只选中一个复选框,然后为选中的输入添加类
- 将 html 输入添加到表中
- 将表单中的输入添加到同一页面中的表中,而无需重新加载
- 将用户输入添加到数组中并在单击时显示
- 显示输入,然后将输入添加到剪贴板ie11
- Javascript:根据用户输入添加到静态URL;添加#xyz
- 通过jquery为mvc项目中动态添加的输入添加验证规则
- 未将标准输入添加到Angularjs Form$Scope中
- jQuery在单击时向输入添加文本
- 当使用AngularJS将新输入添加到另一个输入字段时,如何清除输入字段中的值
- 复选框将输入添加到文本框总选择和总选择成本
- Blueimp为多个输入添加了上传触发器
- 在JavaScript中单击按钮时将用户输入添加到列表
- 表单:如何为新添加的输入添加验证规则
- 通过单击向输入添加值