2016-03-29 65 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