Add onclick property to input with JavaScript(添加 onclick 属性以使用 JavaScript 进行输入)
问题描述
我正在其中一个事件中创建用户输入:
I am creating a user input at one of the events:
var throwConnectBox = function() {
chat_box = document.getElementById('box');
div = window.parent.document.createElement('div');
input = window.parent.document.createElement('input');
input.type = "submit";
input.value = "Join chat";
input.onclick = "conn.send('$connect
');";
div.appendChild(input);
chat_box.appendChild(div);
}
...但生成的输入没有 onclick 属性.我尝试使用
... but the resulting input does not have onclick property. I tried to use
input.onclick = conn.send('$connect
');
... 相反,但也没有工作.我做错了什么?
... instead, but didn' work either. What am I doing wrong?
推荐答案
试试这个:
input.onclick = function() { conn.send('$connect
'); };
史蒂夫
这篇关于添加 onclick 属性以使用 JavaScript 进行输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!