本指南示範了建立可重複使用的 Web 元件:一個簡單的計數器。 我們將利用自訂元素、Shadow DOM 和 HTML 模板。完成的計數器將具有用於增加和減少顯示數值的按鈕。
此程式碼的完整、可運行版本可以在此處。
先決條件:
熟悉基本的 JavaScript 和對 DOM(文件物件模型)的概念性理解會很有幫助,儘管不是嚴格要求。
項目設定:
建立兩個檔案:counter.html
(包含頁面結構)和 counter.js
(包含自訂元素定義)。
counter.html
(初始結構):
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width"> <title>Counter Component</title> </head> <body> <script src="counter.js"></script> </body> </html>
建立範本(counter.html
- 新增範本):
我們將使用 HTML 範本定義計數器的視覺結構:
<template id="x-counter"> <button id="min-btn">-</button> <span id="counter-display">0</span> <button id="plus-btn">+</button> </template>
該模板不會直接渲染;它充當我們自訂元素的藍圖。
定義自訂元素 (counter.js
):
這段 JavaScript 程式碼定義了計數器的功能:
class XCounter extends HTMLElement { constructor() { super(); this.counter = 0; this.elements = {}; } connectedCallback() { const template = document.getElementById("x-counter"); this.attachShadow({ mode: "open" }); this.shadowRoot.appendChild(template.content.cloneNode(true)); this.elements = { plusBtn: this.shadowRoot.querySelector("#plus-btn"), minBtn: this.shadowRoot.querySelector("#min-btn"), counterDisplay: this.shadowRoot.querySelector("#counter-display") }; this.displayCount(); this.elements.plusBtn.onclick = () => this.increment(); this.elements.minBtn.onclick = () => this.decrement(); } increment() { this.counter++; this.displayCount(); } decrement() { this.counter--; this.displayCount(); } displayCount() { this.elements.counterDisplay.textContent = this.counter; } } customElements.define("x-counter", XCounter);
這個類別擴充了HTMLElement
。 connectedCallback
處理元素新增到頁面時的設置,包括附加影子 DOM 和事件監聽器。 increment
、decrement
和 displayCount
管理計數器的值和顯示。
使用計數器元件(counter.html
- 新增自訂元素):
要使用計數器,只需將 <x-counter></x-counter>
加入您的 HTML 即可。
設定元件樣式(counter.js
- 新增樣式):
使用 adoptedStyleSheets
將樣式封裝在組件內:
connectedCallback() { // ... (previous code) ... const sheet = new CSSStyleSheet(); sheet.replaceSync(this.styles()); this.shadowRoot.adoptedStyleSheets = [sheet]; // ... (rest of connectedCallback) ... } styles() { return ` :host { display: block; border: dotted 3px #333; width: fit-content; height: fit-content; padding: 15px; } button { border: solid 1px #333; padding: 10px; min-width: 35px; background: #333; color: #fff; cursor: pointer; } button:hover { background: #222; } span { display: inline-block; padding: 10px; width: 50px; text-align: center; } `; }
這增加了包含在影子 DOM 中的基本樣式。
結論:
本教學示範了建立一個簡單的、可重複使用的 Web 元件。 模板、shadow DOM 和自訂元素的使用促進了 Web 開發的模組化和可維護性。請記住將 [here](https://www.php.cn/link/2eac42424d12436bdd6a5b8a88480cc3)
替換為最終程式碼的實際連結。
以上是如何從頭開始建立簡單的 Web 元件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavaScript核心數據類型在瀏覽器和Node.js中一致,但處理方式和額外類型有所不同。 1)全局對像在瀏覽器中為window,在Node.js中為global。 2)Node.js獨有Buffer對象,用於處理二進制數據。 3)性能和時間處理在兩者間也有差異,需根據環境調整代碼。

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python和JavaScript的主要區別在於類型系統和應用場景。 1.Python使用動態類型,適合科學計算和數據分析。 2.JavaScript採用弱類型,廣泛用於前端和全棧開發。兩者在異步編程和性能優化上各有優勢,選擇時應根據項目需求決定。

選擇Python還是JavaScript取決於項目類型:1)數據科學和自動化任務選擇Python;2)前端和全棧開發選擇JavaScript。 Python因其在數據處理和自動化方面的強大庫而備受青睞,而JavaScript則因其在網頁交互和全棧開發中的優勢而不可或缺。

Python和JavaScript各有優勢,選擇取決於項目需求和個人偏好。 1.Python易學,語法簡潔,適用於數據科學和後端開發,但執行速度較慢。 2.JavaScript在前端開發中無處不在,異步編程能力強,Node.js使其適用於全棧開發,但語法可能複雜且易出錯。

javascriptisnotbuiltoncorc; sanInterpretedlanguagethatrunsonenginesoftenwritteninc.1)JavascriptwasdesignedAsignedAsalightWeight,drackendedlanguageforwebbrowsers.2)Enginesevolvedfromsimpleterterpretpretpretpretpreterterpretpretpretpretpretpretpretpretpretcompilerers,典型地,替代品。

JavaScript可用於前端和後端開發。前端通過DOM操作增強用戶體驗,後端通過Node.js處理服務器任務。 1.前端示例:改變網頁文本內容。 2.後端示例:創建Node.js服務器。

選擇Python還是JavaScript應基於職業發展、學習曲線和生態系統:1)職業發展:Python適合數據科學和後端開發,JavaScript適合前端和全棧開發。 2)學習曲線:Python語法簡潔,適合初學者;JavaScript語法靈活。 3)生態系統:Python有豐富的科學計算庫,JavaScript有強大的前端框架。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver Mac版
視覺化網頁開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

記事本++7.3.1
好用且免費的程式碼編輯器

WebStorm Mac版
好用的JavaScript開發工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。