删除is =""的预期行为是什么?自定义元素的属性?

时间:2016-03-17 20:25:53

标签: javascript html polymer web-component custom-element

例如,

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

document.registerElement('awesomebutton', AwesomeButton)

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

1 个答案:

答案 0 :(得分:7)

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

  

在实例化自定义元素后,更改is属性的值不得影响此元素的自定义元素类型。

但是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)指定:

  

创建自定义元素后,更改is属性的值不会更改元素的行为。