HTML 属性是修改 HTML 元素行为的特殊单词。它们在元素的开始标签内使用,可以修改元素的默认功能或提供必要的功能。从语法上讲,属性被添加到 HTML 开始标记中。它们可以分类为必需、可选、标准或事件属性,并以名称-值对的形式编写,在元素的开始标记内用等号“=”分隔。
下面给出了不同的 HTML 属性及其详细工作原理:
主要使用的核心属性有四个:
代码:
<html> <head> <title>src Attribute</title> </head> <body> <img src=" https://www.google.com/url?sa=i&source=images&cd=&cad=rja&uact=8&ved=2ahUKEwi2lr-WjbvhAhXPXisKHb6JABgQjRx6BAgBEAU&url=https%3A%2F%2Fwww.google.com.mx%2F&psig=AOvVaw2jWnG-ltpLO7QE_Ge7TXeO&ust=1554627554684449"> </body> </html>
示例:
代码:
<html> <head> <title>Width and Height</title> </head> <body> <img src=" https://www.google.com/url?sa=i&source=images&cd=&cad=rja&uact=8&ved=2ahUKEwi2lr-WjbvhAhXPXisKHb6JABgQjRx6BAgBEAU&url=https%3A%2F%2Fwww.google.com.mx%2F&psig=AOvVaw2jWnG-ltpLO7QE_Ge7TXeO&ust=1554627554684449" width="300px" height="100px"> </body> </html>
HTML provides custom data attributes that allow adding additional information related to the content in HTML tags. These attributes are not specific to HTML5 and can be used on all HTML elements. The data-* attribute enables the creation of custom data attributes that can store private data for the page or application.
In order to customize, one divides the data into two parts:
The syntax for a data attribute is as follows:
<li data-book-author="Rabindra Nath Tagore"> Gitanjali </li>
To retrieve the NamedNodeMap objects, one can use the attribute properties in the HTML DOM. This will return a group of node attributes. The NamedNodeMap represents a collection of attribute objects, which can be accessed by their index number, starting at 0. To use this, the user can access the node’s attributes using the syntax node.attributes.
The value returned is a NamedNodeMap object that contains all the attributes in the collection of nodes. However, if someone is using Internet Explorer 8 or an earlier version, the attributes property may return all possible attributes for any element, resulting in more values than expected.
Example:
Code:
<!DOCTYPE html> <html> <head> <title> HTML DOM attributes Property </title> </head> <body> <h2> HTML DOM attributes Property </h2> <button id = "CBA" onclick = "myeduCBA()"> Click Here! </button> <br><br> <span> Button element attributes: </span> <span id="sudo"></span> <script> function myeduCBA() { // It returns the number of nodes var cba = document.getElementById("CBA").attributes.length; // Display the number of nodes document.getElementById("sudo").innerHTML = cba; } </script> </body> </html> The output for above program will be Button element attributes: 2
HTML also provides global attributes that can work with any HTML element:
HTML has the ability to trigger actions when some events take place.
Actions inside an HTML form trigger these events.
HTML 随着时间的推移不断发展,提供了核心、国际化、数据、全局和事件等广泛的属性,使 Web 应用程序和页面更具交互性。与 CSS 相结合,HTML 可以轻松自定义 Web 元素,以创建具有视觉吸引力的 Web 应用程序。
以上是HTML 属性的详细内容。更多信息请关注PHP中文网其他相关文章!