一個用例是創建一個自定義頁面警報組件
<page-alert data-alertHeading='You are in danger' data-alertInfo ="This is a description of why you are in danger.">
</page-alert>
通常,模板化的 HTML 將添加到頁面中使用組件代碼的位置:
this.attachShadow({mode: 'open' });
this.shadowRoot.appendChild(template.content.cloneNode(true));
但是,我想將此自定義組件附加到正文,以便它是開始正文標記之后的第一個元素,并且始終出現在頁面頂部。我怎樣才能做到這一點?
uj5u.com熱心網友回復:
從您的組件內部:
document.body.insertBefore(this, document.body.firstChild);
// or, for evergreen browsers, if you don't care about IE:
document.body.prepend(this);
從外部看,如果您的元素已經在 DOM 中但需要移動:
document.body.insertBefore(document.querySelector('page-alert'), document.body.firstChild);
// or, for evergreen browsers, if you don't care about IE:
document.body.prepend(document.querySelector('page-alert'));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/448702.html
標籤:javascript 网络组件 本机网络组件
上一篇:只畫closePath()線
