在connectedCallback()我的自定义元素的方法中,textContent它作为空字符串返回。
本质上,我的代码可以归结为以下内容...
class MyComponent extends HTMLElement{
constructor() {
super()
console.log(this.textContent) // not available here, but understandable
}
connectedCallback() {
super.connectedCallback() // makes no difference if present or not
console.log(this.textContent) // not available here either, but why?!
}
}
customElements.define('my-component', MyComponent);
Run Code Online (Sandbox Code Playgroud)
还有HTML ...
<my-component>This is the content I need to access</my-component>
Run Code Online (Sandbox Code Playgroud)
从阅读方面看,connectedCallback()这听起来好像是在将元素添加到DOM后就被调用了,所以我希望textContent属性应该是有效的。
我正在使用Chrome 63,如果有帮助...
javascript web-component web custom-element native-web-component