【发布时间】:2020-02-09 09:00:15
【问题描述】:
我尝试使用 JavaScript 在 HTML 中创建自定义标签。我想使用 ES6 JavaScript 语法创建自定义元素。我编写了这段代码来创建自定义元素:
customElements.define('neo-element', NeoElement);
function NeoElement (){
var ref = Reflect.construct(HTMLElement,[], this.constructor) ;
return ref;
};
NeoElement.prototype = Object.create(HTMLElement.prototype);
NeoElement.prototype.constructor = NeoElement;
NeoElement.prototype.connectedCallback = function(){
this.innerHTML = `<h1>Hello world</h1>`;
};
<neo-element></neo-element>
我已经验证 NeoElement 正在正确扩展 HTMLElement,但在 <neo-element> 标记内仍然没有打印任何内容。
谁能看看代码并告诉我 ES5 语法中缺少什么?
【问题讨论】:
标签: javascript html css ecmascript-6 ecmascript-5