如何创建自定义元素的扩展类的新实例 [英] How to create new instance of an extended class of custom elements

查看:26
本文介绍了如何创建自定义元素的扩展类的新实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试 google 开发者网站 并且我收到错误:类型错误:非法构造函数.出了什么问题以及如何解决?

I'm trying the example from google developer site and I'm getting Error: "TypeError: Illegal constructor. What's wrong and How to fix it?

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;

推荐答案

Blink,当前实现自定义元素 v1 的 Web 引擎(在 例如 Chrome v53+)仅支持 自主自定义元素:参见打开闪烁错误.

Blink, the web engine that currently implements Custom Element v1 (in Chrome v53+ for example) only supports autonomous custom elements: see open Blink bug.

如果你想定义 定制的内置元素(即

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆