【问题标题】:How to get custom element member variable from inline event handler?如何从内联事件处理程序中获取自定义元素成员变量?
【发布时间】:2020-01-19 20:02:38
【问题描述】:

因为我想保持自定义元素自包含,所以我想避免在window 对象上放置对成员变量的引用。

customElements.define( 'component-one', class extends HTMLElement {
    connectedCallback() {
        this.memberVariable="This is a member variable";
        this.innerHTML = "<button onclick='//How do I reference the above this.memberVariable here?'>This is a button</button>";
    }
}):

【问题讨论】:

标签: javascript html web-component custom-element native-web-component


【解决方案1】:

我相信这样的事情会有所帮助-:

customElements.define( 'component-one', class extends HTMLElement {
    connectedCallback() {
        var self = this;
        this.memberVariable="This is a member variable";
        this.innerHTML = "<button onclick='self.memberVariable // use this variable as required'>This is a button</button>";
    }
}):

【讨论】:

  • 我测试了这段代码,似乎self 在事件处理程序中引用了window。你能让这段代码工作吗?
猜你喜欢
  • 2012-10-13
  • 2016-12-18
  • 1970-01-01
  • 2013-02-03
  • 2020-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多