我想通過構造函數通過JavaScript創建Button。一切正常,但onclick事件啓動後立即加載頁面,而不是點擊按鈕後。Javascript onclick事件在重新加載頁面時開始
function Button(text) {
this.button = document.createElement('button');
this.button.id = text;
this.button.innerHTML = text;
this.button.style.width = 100;
this.button.style.height = 30;
document.body.appendChild(this.button);
};
b1 = new Button('button1');
b1.onclick = alert('hello');