目錄
Approaches
方法:使用“data-前缀方法”
Example
Employee Table
方法:使用“自定义前缀方法”
Tutorials point
Conclusion
首頁 web前端 html教學 如何在HTML中聲明自訂屬性?

如何在HTML中聲明自訂屬性?

Aug 21, 2023 am 08:37 AM
html 自訂屬性 聲明

如何在HTML中聲明自訂屬性?

In this article, we will discuss how to declare a custom attribute in HTML. Custom attributes can be useful in HTML when you want to store some additional information that is not part of the standard HTML attributes. It allows for more flexibility and customization in HTML and can help make your code more maintainable and understandable.

Approaches

We have two different approaches to declaring a custom attribute in HTML including the following −

  • 使用“ data- 前缀”

  • 使用“自定义前缀”

让我们详细看一下每个步骤。

方法:使用“data-前缀方法”

第一种方法是在HTML中声明一个自定义属性,使用“data-”前缀。我们可以通过在属性名前加上"data-"来定义自定义属性。您可以使用自己选择的前缀来创建自定义属性的名称。这样可以将特定数据附加到HTML元素上,可以很容易地使用CSS、JavaScript或jQuery来定位。

Example

以下是使用“data-前缀”在HTML中声明自定义属性的示例。

<!DOCTYPE html>
<html>
<head>
   <title>Employee Table</title>
   <style>
      h1 {
         color: blue;
      }
      th {
         color: red;
      }
      table {
         border-collapse: collapse;
         width: 100%;
      }
      th, td {
         padding: 5px;
         text-align: left;
         border-bottom: 1px solid #ddd;
      }
      tr:nth-child(even) {
         background-color: #f2f2f2;
      }
   </style>
</head>
<body>
   <h1 id="Employee-Table">Employee Table</h1>
   <table>
      <thead>
         <tr>
            <th>Employee ID</th>
            <th>Name</th>
            <th>Department</th>
            <th>Salary</th>
         </tr>
      </thead>
      <tbody>
         <tr data-empid="001" data-dept="IT" data-salary="75000">
            <td>001</td>
            <td>Taylor</td>
            <td>IT</td>
            <td>$75,000</td>
         </tr>
         <tr data-empid="002" data-dept="HR" data-salary="60000">
            <td>002</td>
            <td>Jhon</td>
            <td>HR</td>
            <td>$60,000</td>
         </tr>
      </tbody>
   </table>
   <script>
      const rows = document.querySelectorAll("tr[data-empid]");
      rows.forEach(row => {
         const empid = row.dataset.empid;
         const dept = row.dataset.dept;
         const salary = row.dataset.salary;
         console.log(`Employee ID: ${empid}, Department: ${dept}, Salary: ${salary}`);
      });
   </script>
</body>
</html> 
登入後複製

Note − Here, JavaScript can access the custom attributes in the example code by selecting the "tr" elements based on their custom attributes, and then logging the values of those attributes to the console.

方法:使用“自定义前缀方法”

第一种方法是在HTML中声明一个自定义属性作为“自定义前缀”。在HTML中,自定义前缀指的是在HTML元素上创建自定义属性。前缀可以是您选择的任何名称,添加在属性名称之前,它可以用于向元素添加附加数据或信息。通过使用自定义前缀,您可以创建适用于您需求的自定义属性,并可以在CSS或JavaScript中进行定位。

Example

Following is an example of declaring a custom attribute in HTML using “custom prefix”.

<!DOCTYPE html>
<html>
<head>
   <title>Custom Attribute Example</title>
   <style>
      [custom] {
         color: red;
      }
   </style>
</head>
<body>
   <h1 id="Tutorials-point">Tutorials point</h1>
   <p custom="paragraph-color">Hello World!</p>
   <script>
      const header = document.querySelector("[custom='header-color']");
      const paragraph = document.querySelector("[custom='paragraph-color']")
      console.log(`Header color: ${header.getAttribute('custom')}`);
      console.log(`Paragraph color: ${paragraph.getAttribute('custom')}`);
   </script>
</body>
</html>
登入後複製

It defines a custom attribute named "custom" and applies the CSS style to any element with that attribute.The JavaScript code selects the header and paragraph elements based on their custom attributes and logs the values of those attributes.

Conclusion

在本文中,我们研究了在HTML中声明自定义属性的两种不同方法。在这里,我们使用了“data-前缀”和“自定义前缀”这两种不同的方法。自定义前缀和data-前缀都用于在HTML中声明自定义属性,但是data-前缀更广泛被认可,并且被所有现代浏览器支持。自定义前缀更灵活,因为它允许您选择自己的前缀名称,但是它可能不被所有浏览器或工具认可。

以上是如何在HTML中聲明自訂屬性?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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

熱門話題

Java教學
1665
14
CakePHP 教程
1424
52
Laravel 教程
1322
25
PHP教程
1270
29
C# 教程
1250
24
HTML 中的表格邊框 HTML 中的表格邊框 Sep 04, 2024 pm 04:49 PM

HTML 表格邊框指南。在這裡,我們以 HTML 中的表格邊框為例,討論定義表格邊框的多種方法。

HTML 中的巢狀表 HTML 中的巢狀表 Sep 04, 2024 pm 04:49 PM

這是 HTML 中巢狀表的指南。這裡我們討論如何在表中建立表格以及對應的範例。

HTML 左邊距 HTML 左邊距 Sep 04, 2024 pm 04:48 PM

HTML 左邊距指南。在這裡,我們討論 HTML margin-left 的簡要概述及其範例及其程式碼實作。

HTML 表格佈局 HTML 表格佈局 Sep 04, 2024 pm 04:54 PM

HTML 表格佈局指南。在這裡,我們詳細討論 HTML 表格佈局的值以及範例和輸出。

HTML 輸入佔位符 HTML 輸入佔位符 Sep 04, 2024 pm 04:54 PM

HTML 輸入佔位符指南。在這裡,我們討論 HTML 輸入佔位符的範例以及程式碼和輸出。

您如何在PHP中解析和處理HTML/XML? 您如何在PHP中解析和處理HTML/XML? Feb 07, 2025 am 11:57 AM

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

HTML 有序列表 HTML 有序列表 Sep 04, 2024 pm 04:43 PM

HTML 有序列表指南。在這裡我們也分別討論了 HTML 有序列表和類型的介紹以及它們的範例

HTML onclick 按鈕 HTML onclick 按鈕 Sep 04, 2024 pm 04:49 PM

HTML onclick 按鈕指南。這裡我們分別討論它們的介紹、工作原理、範例以及各個事件中的onclick事件。

See all articles