简体   繁体   English

DOM操作甚至处理程序

[英]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. 我不知道如何在单击时告诉该按钮转到功能MarkCell()。 I know that I will use the onClick Event but I don't know how to tell it what onClick event. 我知道我将使用onClick事件,但是我不知道如何告诉它什么onClick事件。 I have to use DOM manipulation. 我必须使用DOM操作。

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. 您可以使用addEventListener方法来附加事件处理程序。 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);  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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