简体   繁体   English

如何发送按钮到输入框?

[英]How to send buttons to input box?

I'm trying to send buttons from end result to new input box but have no idea. 我试图将按钮从最终结果发送到新的输入框,但不知道。 I have tried several things. 我已经尝试了几件事。

First 第一

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";
}
}

Second 第二

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";
}
}

But they dont work. 但是他们不起作用。 Thank you in advance. 先感谢您。

Your variations on the code are failing, but in different ways. 您对代码的修改失败了,但是方式不同。 The first fails because it adds the onclick to the wrong element, and the second fails because it does not properly set the onclick . 第一个失败是因为它将onclick添加到错误的元素,第二个失败是因为未正确设置onclick Use the following instead of the text after document.body.appendChild(newbutton); 使用以下内容代替document.body.appendChild(newbutton);之后的文本document.body.appendChild(newbutton); :

  var ansFunction = function() {
    document.getElementById("demo").innerHTML = "answers";
  }
  newbutton.addEventListener("click", ansFunction);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM