簡體   English   中英

內部onclick函數不起作用

[英]Inside onclick function not working

在這里我正在onclick中創建onclick函數,現在可以工作

element.onclick = function(e) {
var div = document.getElementById("message");
var div1= document.getElementById("send");
var input = document.createElement("textarea");
var button = document.createElement("input");
button.setAttribute("type", "submit");
button.setAttribute("value", "send");
button.setAttribute("id", candidateId);
input.name = "post";
input.maxLength = "500";
input.cols = "30";
input.rows = "10";
div.appendChild(input); //appendChild
div.appendChild(button);
console.log(button)
button.onclick = function(e) {

alert("comming")
}                       

}

這里的button.onclick功能沒有被觸發,任何人都可以幫我..

在代碼中,您僅綁定事件。 如果要觸發它,則在綁定后觸發它:

element.onclick = function(e) {
  var div = document.getElementById("message");
  var div1 = document.getElementById("send");
  var input = document.createElement("textarea");
  var button = document.createElement("input");
  button.setAttribute("type", "submit");
  button.setAttribute("value", "send");
  button.setAttribute("id", candidateId);
  input.name = "post";
  input.maxLength = "500";
  input.cols = "30";
  input.rows = "10";
  div.appendChild(input); //appendChild
  div.appendChild(button);
  console.log(button)
  button.onclick = function(e) { // <-----binding is here
    alert("comming")
  }

  button.click(); // <-----trigger it here

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM