我可以在自定义 HTML 元素中使用标准属性吗?
Can I use standard attributes in custom HTML elements?
我可以在自定义元素中使用标准属性吗(如果它们仅扩展 HTMLElement)?或者我必须为它们使用 data-
自定义属性?
Section 4.13.3 of the HTML spec 包含可与自治自定义元素(通常扩展 HTMLElement
而不是其他一些现有元素)一起使用的属性列表:
Content attributes:
Global attributes, except the is
attribute
form
, for form-associated custom elements — Associates the element with a form
element
disabled
, for form-associated custom elements — Whether the form control is disabled
readonly
, for form-associated custom elements — Whether to allow the value to be edited by the user
name
, for form-associated custom elements — Name of the element to use for form submission and in the form.elements
API
Any other attribute that has no namespace (see prose).
值得注意的是,自定义元素可以具有您想要的任何属性名称,但除了上面列出的那些之外,如果这些属性碰巧出现,它们将不会具有您期望从内置元素获得的任何特殊行为对应于熟悉的属性(例如 type
)。当然,您需要自己定义此行为。
您只需要在扩展内置元素并想要重新利用现有属性的名称时使用自定义数据属性。
我可以在自定义元素中使用标准属性吗(如果它们仅扩展 HTMLElement)?或者我必须为它们使用 data-
自定义属性?
Section 4.13.3 of the HTML spec 包含可与自治自定义元素(通常扩展 HTMLElement
而不是其他一些现有元素)一起使用的属性列表:
Content attributes:
Global attributes, except theis
attribute
form
, for form-associated custom elements — Associates the element with aform
element
disabled
, for form-associated custom elements — Whether the form control is disabled
readonly
, for form-associated custom elements — Whether to allow the value to be edited by the user
name
, for form-associated custom elements — Name of the element to use for form submission and in theform.elements
API
Any other attribute that has no namespace (see prose).
值得注意的是,自定义元素可以具有您想要的任何属性名称,但除了上面列出的那些之外,如果这些属性碰巧出现,它们将不会具有您期望从内置元素获得的任何特殊行为对应于熟悉的属性(例如 type
)。当然,您需要自己定义此行为。
您只需要在扩展内置元素并想要重新利用现有属性的名称时使用自定义数据属性。