元素HTMLSlotElement
的name属性, 可以获取和设置属性值. 插槽提供了web组件的摆放位置,辅助用户布局。
语法
var name = htmlSlotElement.name htmlSlotElement.name = name
值
一个 DOMString
.
例子
下面的片段来自 slotchange example (see it live also).
let slots = this.shadowRoot.querySelectorAll('slot');
slots[1].addEventListener('slotchange', function(e) {
let nodes = slots[1].assignedNodes();
console.log('Element in Slot "' + slots[1].name + '" changed to "' + nodes[0].outerHTML + '".');
});
这里我们获取所有插槽的引用, 然后给模板里的第二个插槽增加事件— 跟踪内容变化.
每当附加在插槽中的内容变化, 我们都记录插槽相关的内容.
规格
规格 | 状态 | 注释 |
---|---|---|
HTML Living Standard name |
Living Standard |
浏览器兼容性
BCD tables only load in the browser
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.