【发布时间】:2017-02-20 12:20:42
【问题描述】:
我正在尝试来自 google developer site 的示例,但出现错误:“TypeError: Illegal constructor. 出了什么问题以及如何解决?
class FancyButton extends HTMLButtonElement {
constructor() {
super(); // always call super() first in the ctor.
this.addEventListener('click', e => this.drawRipple(e.offsetX,e.offsetY));
}
// Material design ripple animation.
drawRipple(x, y) {
let div = document.createElement('div');
div.classList.add('ripple');
this.appendChild(div);
// div.style.top = `${y - div.clientHeight/2}px`;
// div.style.left = `${x - div.clientWidth/2}px`;
div.style.backgroundColor = 'currentColor';
div.classList.add('run');
div.addEventListener('transitionend', e => div.remove());
}
}
customElements.define('fancy-button', FancyButton, {extends: 'button'});
let button = new FancyButton();
button.textContent = 'Fancy button!';
button.disabled = true;
【问题讨论】:
-
您使用的是什么浏览器(版本)?
-
当前浏览器都不支持Custom Elements v1。
-
来自开发者网站:Chrome 54(状态)具有自定义元素 v1。 Safari 已经开始原型设计,您可以每晚在 WebKit 中测试 API。 Edge 已经开始原型设计。 Mozilla 有一个开放的错误要实施。
-
Safari 不会在第一次 AFAIK 中实现自定义的内置元素
标签: javascript html ecmascript-6 web-component custom-element