自訂 Html 元素
以下文章提供了自訂 Html 元素的概述。在html中,我們有許多Web元件的功能;有些具有建立使用者定義或自訂 html 元素的標準能力。它以 html 語言封裝了網頁以提供更多功能。使用一組巢狀的批次元素需要很長時間,這些元素可以與更多自訂網頁功能結合。一些網頁瀏覽器支援自訂元素,例如Mozilla、firefox、google chrome 和Microsoft Edge 瀏覽器;它們支援html 自訂元素、safari 和opera;這些瀏覽器與html 自訂元素不相容;它僅支援自主的用戶定義元素。
文法:
我們將使用 javascript 來定義新的 html 元素,就像自訂元素一樣,因為它是一個全球化的元素,用於在 html 中引入新標籤。因此,當我們使用網頁元素時,語法會有所不同。
Class sample extends HtmlElement { default constructor() { ---some user defined codes--- } }
以上程式碼是基於java的範例程式碼;這是建立自訂元素的大綱,變更會受網頁影響。
在哪裡使用自訂 Html 元素?
一般html自訂元素包含兩種類型:自主自訂元素和自訂內建元素。 每當我們在 HTML 中建立自訂元素時,它都會描述類別及其方法、屬性和屬性;有些事件也這樣稱呼。一旦建立了自訂元素,並將其定義為內建的 html 元素,例如
自治自訂元素包含所有新元素以及使用 HtmlElement 類別擴充的使用者定義元素;它將附帶java標準規則。此外,Customizedbuilt-inElements會建立內建元素,在自治自訂元素中建立自訂元素;每當從網頁中新增或刪除元素時,我們都會告訴瀏覽器如何顯示它。
自主自訂元素使用具有特殊方法的類別來實現上述場景。例如,一些方法是“connectedCallback()”,每當將元素新增至文件時,該方法將用於瀏覽器呼叫。此外,如果在 html 文件中重複新增或刪除該元素,則可以多次呼叫它。 「每當從文件中刪除元素時,此方法都會呼叫瀏覽器;disconnectedCallback()」也可以多次呼叫它來在html文件中重複新增或刪除元素。
observedAttributes() 是傳回屬性名稱陣列以監視反射的變更的方法之一。 attributeChangedCallback(name,oldvalue,newvalue) 方法在將列出並修改任何一個屬性時調用,並且「每當元素移動到html 文件中的新元素時,就會調用「adoptedCallback()」。現在,假設我們使用任何html 元素。使用上面提到的方法來呼叫所需的方法,我們將使用javascript 在網頁中使用它們的功能。
假設我們在 html 中使用一些預設標籤(例如 time>)來計算日期和時間是時間的標籤元素。不過,當時它並沒有自動設定任何時間格式;我們將使用像connectedCallback()這樣的方法;此方法將透過呼叫建立自訂元素後還需要升級整個格式,例如我們PC上的時間更新,但它會在腳本中未使用customElements.define方法中的html元素之前更新,因為這不是錯誤;該元素顯示為未知,就像我們所說的非標準html 標籤一樣;之後,它將在css 樣式選擇器中使用,例如
:not(:define) ,然後調用customElements.define 方法,並且它將在它將支援的時間格式選項中升級新實例。 connectCallback() 方法也被調用,然後它們變成:定義的狀態,就像稱為 customElements.get(name)、customElements.whenDefined(name) 的方法一樣,這兩個方法都會傳回名稱作為參數。
Examples of Custom Html Element
Different examples are mentioned below:
Example #1
<html> <head> <script> class sample extends HTMLElement { // (1) connectedCallback() { let d = new Date(this.getAttribute('datetime') || Date.now()); this.innerHTML = new Intl.DateTimeFormat("default", { month: this.getAttribute('month') || undefined, day: this.getAttribute('day') || undefined, year: this.getAttribute('year') || undefined, minute: this.getAttribute('minute') || undefined, hour: this.getAttribute('hour') || undefined, timeZoneName: this.getAttribute('time-zone-name') || undefined, second: this.getAttribute('second') || undefined, }).format(d); } } customElements.define("time-formatted", sample); </script> </head> <time-formatted datetime="2020-02-19" year="numeric" month="long" day="numeric" hour="numeric" minute="numeric" second="numeric" time-zone-name="long"> </time-formatted> </html>
Output:
Example #2
<html> <head> <script> customElements.define('user-information', class extends HTMLElement { connectedCallback() { alert(this.innerHTML); } }); </script> </head> </html> <user-information>Sivaraman</user-information>
Output:
Example #3
<html> <head> <script> class Example extends HTMLButtonElement { constructor() { super(); this.addEventListener('click', () => alert("User!")); } } customElements.define('sample-button', Example, {extends: 'button'}); </script> <button is="sample-button">Welcome</button> <button is="sample-button" disabled>Disabled</button> </head> </html>
Output:
The above three examples will discuss the custom elements in the html languages; In the first example, we already know about the time and date format output using custom tag elements; the second example shows a basic javascript function called after executing the custom elements in the html and final example will be discussed about the same javascript function while we are clicking the html custom tag elements.
Conclusion
The Web components have some processes for connecting with the technologies. It will be used to help the html for reusable purposes across the entire web.Html have the Dom components; it will be used for communicating the user-level data(including custom elements) through the web for data migration.
以上是自訂 Html 元素的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

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

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

本教程演示瞭如何使用PHP有效地處理XML文檔。 XML(可擴展的標記語言)是一種用於人類可讀性和機器解析的多功能文本標記語言。它通常用於數據存儲
