Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. HTMLSlotElement
  4. assign()

HTMLSlotElement: assign() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨March 2023⁩.

Theassign() method of theHTMLSlotElement interface sets the slot'smanually assigned nodes to an ordered set of slottables. The manually assigned nodes set is initially empty until nodes are assigned usingassign().

Note:You cannot mix manually (imperative) and named (declarative, automatic) slot assignments. Therefore, for this method to work, the shadow tree needs to have beencreated with theslotAssignment: "manual" option.

Syntax

js
assign(node1)assign(node1, node2)assign(node1, node2, /* …, */ nodeN)

Parameters

node1, …,nodeN

A set ofElement orText nodes.

Return value

None (undefined).

Exceptions

NotAllowedErrorDOMException

Thrown when calling this method on an automatically assigned slot.

Examples

In the example below, theassign() method is used to display the correct tab in a tabbed application. The function is called and passed the panel to show, which is then assigned to the slot.

js
function UpdateDisplayTab(elem, tabIdx) {  const shadow = elem.shadowRoot;  const slot = shadow.querySelector("slot");  const panels = elem.querySelectorAll("tab-panel");  if (panels.length && tabIdx && tabIdx <= panels.length) {    slot.assign(panels[tabIdx - 1]);  } else {    slot.assign();  }}

Specifications

Specification
HTML
# dom-slot-assign

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp