简体   繁体   中英

DOM manipulation even handlers

I want to add an Event handler to this button I created with an event handler. I don't know how to tell this button to go to a function MarkCell() when clicked though. I know that I will use the onClick Event but I don't know how to tell it what onClick event. I have to use DOM manipulation.

var right = document.createElement("Button");
var t = document.createTextNode("RIGHT");
right.appendChild(t);
document.body.appendChild(right);  

You can use the addEventListener method to attach an event handler. For example...

function markCell() {
    console.log("markCell called.");
}

var right = document.createElement("Button");
var t = document.createTextNode("RIGHT");
right.appendChild(t);
right.addEventListener("click", markCell)
document.body.appendChild(right);  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM