Creating Dynamic button with click event in JavaScript

前端 未结 4 1558
栀梦
栀梦 2020-11-28 07:15

How can I create a dynamic button with a click event with JavaScript?

I tried this, but when I click the add button, an alert message show up! It\'s not what I want

4条回答
  •  情歌与酒
    2020-11-28 08:21

    this:

    element.setAttribute("onclick", alert("blabla"));
    

    should be:

    element.onclick = function () {
      alert("blabla");
    }
    

    Because you call alert instead push alert as string in attribute

提交回复
热议问题