assignedSlot 只读特性在 Slottable interface returns an HTMLSlotElement 表示 <slot> element the node is inserted in.

句法

var slotElement = elementInstance.assignedSlot
					

HTMLSlotElement 实例,或 null if the element is not assigned to a slot, or if the associated shadow root was attached with its mode 设为 closed (见 Element.attachShadow for further details).

范例

In our simple-template example ( see it live ), we create a trivial custom element example called <my-paragraph> in which a shadow root is attached and then populated using the contents of a template that contains a slot named my-text .

<my-paragraph> is used in the document, the slot is populated by a slottable element by including it inside the element with a slot attribute with the value my-text . Here is one such example:

<my-paragraph>
  <span slot="my-text">Let's have some different text!</span>
</my-paragraph>
					

In our JavaScript file we get a reference to the <span> shown above, then log a reference to the original <slot> element the <span> was inserted in.

let slottedSpan = document.querySelector('my-paragraph span')
console.log(slottedSpan.assignedSlot); // logs '<slot name="my-text">'
					

规范

规范 状态 注释
DOM
The definition of 'assignedSlot' in that specification.
实时标准 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
assignedSlot Chrome 53 Edge 79 Firefox 63
63
不支持 59 — 63 Disabled
Disabled From version 59 until version 63 (exclusive): this feature is behind the dom.webcomponents.shadowdom.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
不支持 ? — 59 Disabled
Disabled Until version 59 (exclusive): this feature is behind the dom.webcomponents.enabled preference (needs to be set to true ) 和 preference (needs to be set to ). To change preferences in Firefox, visit about:config.
IE No Opera 40 Safari 10.1 WebView Android 53 Chrome Android 53 Firefox Android 63
63
不支持 59 — 63 Disabled
Disabled From version 59 until version 63 (exclusive): this feature is behind the dom.webcomponents.shadowdom.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
不支持 ? — 59 Disabled
Disabled Until version 59 (exclusive): this feature is behind the dom.webcomponents.enabled preference (needs to be set to true ) 和 preference (needs to be set to ). To change preferences in Firefox, visit about:config.
Opera Android 41 Safari iOS 10.3 Samsung Internet Android 6.0

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

用户必须明确启用此特征。

用户必须明确启用此特征。

元数据

  • 最后修改:
  1. Slottable
  2. 特性
    1. Slottable: assignedSlot