///// FORM  ////
var form = document.getElementById("form");
form.setAttribute("style", "position: relative; margin-bottom: 5%;");

var newButton = document.createElement('button');
var newUl = document.createElement('ul');
var newLi = document.createElement('li');
var newInput = document.createElement('input');
form.appendChild(newButton);
newButton.setAttribute("style", "position: absolute; width: 15%; height: 40px; left: 42%; color: #B00000; background-color: white; font-size: 18px; border: 0px; ");
newButton.innerHTML = "Direct Message";
newButton.onClick = function() {
  alert("works");
};


我的选择有问题吗?只希望on.click在newButton创建的元素上起作用。

最佳答案

JavaScript属性名称区分大小写,应为newButton.onclick

或者,您可以进入现代时代并使用addEventListener

newButton.addEventListener("click", function() {
    alert("works");
});

10-08 19:48