2016-03-29 66 views
0

我在这里创造的onclick函数内部的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功能不开火,任何一个可以帮助我..

+0

https://jsfiddle.net/arunpjohny/g1d26gfL/1/ - >工作正常 –

+0

是否需要自动触发'button.onclick'?手动点击发送按钮时,代码工作正常。 – Munawir

+0

不工作,我在Java脚本中也动态创建元素。 – arjun

回答

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 

} 
+0

不工作,得到同样的问题 – arjun

+0

什么不工作? – Jai

+0

onclick功能不起作用 – arjun