微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 如何创建扩展自定义元素类的新实例

我正在尝试从 google developer site开始的示例,我得到错误:“TypeError:非法构造函数.
怎么了,怎么解决
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,当前实现Custom Element v1的Web引擎(例如 Chrome v53+)仅支持 autonomous custom elements:请参阅打开 Blink bug.

如果要定义customized built-in elements(即< button>扩展名),则需要使用像the one from Web Reflection这样的polyfill.

或者,您仍然可以使用Custom Element v0语法(document.registerElement).

更新

自Chrome v60(2017年7月)以来,alpha implementation定制的buit-in元素可在旗帜下使用.但是它仅适用于< p>和< div>标签

更新2

现在(2018年4月)它本身适用于Chrome v67及以上版本:-)

原文地址:https://www.jb51.cc/js/150409.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐