【发布时间】:2021-07-31 08:45:33
【问题描述】:
我想创建一个动态的 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)
}
``
【问题讨论】:
-
你应该看看web components
-
非常感谢!
-
说实话,这看起来是一样的原理,不是吗?所以,是的,它确实:-D 无法提前找到它...需要提高我的搜索技能...
标签: javascript html object