如何将按钮发送到输入框?
问题描述:
我试图从最终结果发送按钮到新的输入框,但不知道。 我已经尝试了几件事情。如何将按钮发送到输入框?
首先
for (i=0;i<res.length;i++) {
var newbutton = document.createElement("BUTTON");
var t = document.createTextNode(" "+res[i]);
newbutton.appendChild(t);
newbutton.style.marginRight = "10px";
document.body.appendChild(newbutton);
document.getElementById("demo").addEventListener("click",ansFunction);
function ansFunction() {
document.getElementByid("demo").innerHTML = "answers";
}
}
二
for (i=0;i<res.length;i++) {
var newbutton = document.createElement("BUTTON");
var t = document.createTextNode(" "+res[i]);
newbutton.appendChild(t);
newbutton.style.marginRight = "10px";
document.body.appendChild(newbutton);
newbutton.onclick = "ansFunction";
function ansFunction() {
document.getElementByid("demo").innerHTML = "answers";
}
}
,但他们不工作。 预先感谢您。
答
您的代码变体失败,但方式不同。第一个失败是因为它将onclick
添加到错误的元素,第二个失败,因为它没有正确设置onclick
。 document.body.appendChild(newbutton);
后使用以下代替文字:
var ansFunction = function() {
document.getElementById("demo").innerHTML = "answers";
}
newbutton.addEventListener("click", ansFunction);
}
+0
不,我将显示按钮的价值。 我想通了,我不能把按钮放在输入框中,我试图显示单击按钮时的值。 –
'res'的值是多少? – RamenChef
'function myFunction(){ var str = document.getElementById(“input”).value; var res = str.split(“”); shuffle(res); document.getElementById(“demo”)。innerHTML =“”;'这里是。我打乱了输入值 –
并定义了'shuffle()'?如果是这样,它有什么作用? – RamenChef