<分区>
我想创建一个动态的 HTML/JS 用户界面。 为此,我将动态创建对象,这些对象将在我的 html 中表示为一个按钮。 现在,我当然想跟踪点击事件。 如何将 addEventlistener 动态添加到按钮的这个特殊实例? (每个按钮创建自己的 svg 图标) 我的想法是,对象是按钮,但我不确定该怎么做......
我的想法是这样的:
class Lightbulb {
/**
*
* @param {String} id
* @param {boolean} state
*/
constructor(id, state){
this.id = id;
this.state = state;
/*
create the icon here...
*/
//is it somehow possible to achieve the following?
this.addEventlistener("click", this.setState(!this.state));
}
/**
*
* {boolean} newState
*/
setState(newState){
console.log(newState)
}
``