簡體   English   中英

如何發送按鈕到輸入框?

[英]How to send buttons to input box?

我試圖將按鈕從最終結果發送到新的輸入框,但不知道。 我已經嘗試了幾件事。

第一

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);之后的文本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