javascript to create a button with onclick

前端 未结 2 1826
你的背包
你的背包 2020-12-01 09:49

I\'m trying to use javascript to create a button that has a onclick event that calls a function defined in the head that takes in as parameter a dom object relative to the

相关标签:
2条回答
  • 2020-12-01 10:14

    You can also use the built-in setAttrbute javascript function.

    var newButton = document.createElement("button")
    newButton.setAttribute("onclick", "blah(this.parentNode.value)")

    Hope it will help

    0 讨论(0)
  • 2020-12-01 10:18
    function createButton(context, func) {
        var button = document.createElement("input");
        button.type = "button";
        button.value = "im a button";
        button.onclick = func;
        context.appendChild(button);
    }
    
    window.onload = function() {
        createButton(document.body, function() {
            highlight(this.parentNode.childNodes[1]);
            // Example of different context, copied function etc
            // createButton(this.parentNode, this.onclick);
        });
    };
    

    Is that what you want?

    0 讨论(0)
提交回复
热议问题