javascript to create a button with onclick

前端 未结 2 1825
你的背包
你的背包 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: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?

提交回复
热议问题