编程语言
首页 > 编程语言> > javascript – 从自定义元素中删除is =“”属性的预期行为是什么?

javascript – 从自定义元素中删除is =“”属性的预期行为是什么?

作者:互联网

例如,

假设我们有< div is =“awesomebutton”>< / div>和自定义元素定义:

document.registerElement('awesomebutton', AwesomeButton)

删除is =“”属性或用新值替换时的预期行为是什么?

解决方法:

Per section 7 of the latest Working Draft specification (26 February 2016),它应该对元素的类型没有影响:

After a custom element is instantiated, changing the value of the is attribute must not affect this element’s custom element type.

但是仍应正常触发attributeChangedCallback. (规范不会免除is属性触发它.)您可以在支持的浏览器(Chrome或具有dom.webcomponents.enabled配置标志集的Firefox)中观察此行为:

'use strict';

const prototype = Object.create(HTMLElement.prototype);
prototype.attributeChangedCallback = function(name, oldValue, newValue) {
  this.textContent = `my "${name}" attribute changed to "${newValue}"!`; 
};

document.registerElement('examp-el', {prototype: prototype, extends: 'div'});

const el = document.createElement('div', 'examp-el');
el.textContent = "I'm an element!";

document.body.appendChild(el);
el.setAttribute('is', "changed once");
el.removeAttribute('is');
el.setAttribute('is', "changed twice");
el.setAttribute('is', "changed thrice");

该规范尚未标准化,很快将发生重大变化,但我不希望这种行为发生变化.它仍然由Section 2.3 of the latest version of the Editor’s Draft (currently 17 March 2016)指定:

After a custom element is created, changing the value of the is attribute does not change the element’s behavior.

标签:javascript,polymer,web-component,html,custom-element
来源: https://codeday.me/bug/20190528/1167362.html