内部onclick功能无法正常工作

时间:2016-03-29 12:20:21

标签: javascript

这里我正在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函数没有被触发,任何人都可以帮助我..

1 个答案:

答案 0 :(得分:0)

在代码中,您只绑定事件。如果你想解雇它,那么在绑定后触发它:

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

}