Gitanjali <\/li><\/pre>\n5. DOM Attribute Property<\/h4>\n\n\n\n\n\n\n\n\n\n\n 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.<\/p>\n
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.<\/p>\n\n\n\n\n\n\n\n\n\n\n
Example:<\/strong><\/p>\nCode:<\/strong><\/p>\n\n\n\n\n\n\nHTML DOM attributes Property\n<\/title>\n<\/head>\n \n\nHTML DOM attributes Property\n<\/h2>\n\nClick Here!\n<\/button>\n \n\nButton element attributes:\n<\/span>\n<\/span>\n
HTML Attributes
Sep 04, 2024 pm 04:17 PM
html
html5
HTML Tutorial
HTML Properties
HTML tags
HTML attributes are special words that modify the behavior of an HTML element. They come in use within the opening tags of elements and can either modify the default functionality of an element or provide the necessary functionality. Syntactically, an attribute is added to the HTML start tag. They can be categorized as required, optional, standard, or event attributes, and are written as name-value pairs separated by an equal sign ” = ” within an element’s start tag.
?
Different HTML Attributes
Given below are the different HTML attributes and how they work in detail:
1. Core Attributes
There are four core attributes primarily in use:
ID: This attribute identifies an element uniquely within an HTML page. When an element carries an ID attribute, it serves as a unique identifier that makes it easy to identify the element and its content. This is particularly useful when there are multiple elements with the same name within a web page.
Title Attribute: This attribute provides a suggested title for an element. Its behavior depends on the context in which it is used, and it may be displayed as a tooltip when the cursor hovers over the element or when the element is loading. It can also provide additional information about an element when the user hovers the mouse pointer over it.
Class Attribute: This attribute associates an element with a style sheet by specifying the class of the element. More can be learned about this attribute when Cascading Style sheet is being learned. The value of the class attribute can be a space-separated list of class names. For example: class=”className1 className2 className3″
Style Attribute: ?This attribute allows the user to specify CSS rules for an individual element. With the style attribute, the user can apply various CSS effects to HTML elements, such as changing the font size, font family, and color.
2. Internationalization Attribute
Dir: The dir attribute helps indicate to the browser the direction that text should follow. This attribute can take on two values: LTR and RTL. LTR means left to right, which is the default value, while RTL stands for right to left. When used within the tag, it determines how text should be represented throughout the entire document. It can also control the direction of text within the content of the tag.
Lang Attribute: This attribute helps showcase the main language used in a document. One can use it in HTML to maintain backward compatibility with earlier versions of HTML. Also, one replace it by the XML: lang attribute in new XHTML documents. The values of the lang attribute are based on the ISO-639 standard and consist of two-character language codes. Declaring a language is important for accessibility and for search engines to index content properly.
XML-Lang Attribute: This attribute tends to replace the lang attribute. The value of the XML-lang attribute must include the language and country codes as specified by ISO-639.
3. Generic Attribute
Align Attribute: This attribute is useful when you want to position certain elements on your web page. It allows you to change the alignment to the left, right, or center of the page. The default alignment for all elements is set to the left, but you can change it using the align attribute.
Src Attribute: When one wants to insert an image into a web page, one needs to use the tag with the src attribute. One can specify the image’s address as the attribute’s value inside double quotes. One can use the src attribute as follows to include the image on the webpage.
Code:
<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>
Alt Attribute: This attribute is used as an alternative tag that can be used to display something if the primary attribute, which is the tag, fails to display the original image assigned to it. It can describe the image to a developer who is using it at the coding end. If the main image fails, then the alternate image can display.
The Width and Height Attribute: These attributes can adjust the height and width of an image.
Example:
Code:
<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>
4. Data Attribute
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:
Attribute Name: It should have at least one character long and should not have any capital letters. This name can also be prefixed by using “data-“.
Attribute Value: Any string value can be associated with the attribute.
The syntax for a data attribute is as follows:
<li data-book-author="Rabindra Nath Tagore"> Gitanjali </li>
5. DOM Attribute Property
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
6. Global Attributes
HTML also provides global attributes that can work with any HTML element:
Accesskey: Specifies a shortcut key to activate or focus on any element.
Translate: Specifies whether the content of the element is to be translated or not.
Class: One or more class names for an element are specified.
Title: Specifies extra information about an element.
Contenteditable: This attribute can be used to specify whether the content is editable or not.
Tabindex: Specifies the tabbing order of an element.
Dir: Specifies the text direction for any content of an element.
Spellcheck: Users can explicitly specify if they want to have the spelling and grammar checked or not.
Draggable: Specifies if an element should be draggable or not.
Dropzone: Specifies whether the dragged data is copied, moved, or linked when dropped.
7. Event Attributes
HTML has the ability to trigger actions when some events take place.
Onload: Fires after the page has finished loading.
Onmessage: A script that runs when the message is triggered.
Onstorage: A script that runs when a web storage area is updated.
Onerror: The script runs when an error occurs.
Onpagehide: The script can be used when the user navigates away from a page.
Actions inside an HTML form trigger these events.
Onblur: It is triggered as soon as the element loses focus.
Onchange: It is triggered as soon as the value of an element changes.
Oncontextmenu: This is run when a context menu is triggered.
Onfocus: It is triggered as soon as the element gets focus.
Oninput: The script has to run when the element receives an input.
Onsearch: This is triggered when the user writes something in the search field.
Oninvalid: This is triggered when the entered element is invalid.
9. Key Event Attributes
Onkeydown: Triggered when a key is being pressed.
Onkeypress: Triggered when a key is pressed.
Onkeyup: This is triggered when a user releases a key.
10. Mouse Event Attributes
Onclick: This is triggered when the mouse clicks on an element.
Onmousemove: Fired when the mouse pointer is moving while it is over an element.
Onmouseip: Triggered when a mouse button is released from over an element.
On-wheel: Triggered when the mouse wheel rolls up or down over an element
11. Drag Event Attributes
Ondrag: This is triggered when an element is dragged.
Ondragleave: The script runs when an element leaves a valid drop target.
Ondrop: Triggered when the dragged element is being dropped
Onscroll: The script runs when an element’s scroll bar is being scrolled.
Conclusion
HTML has evolved over time, offering a wide range of attributes such as core, internationalization, data, global, and event, making web applications and pages more interactive. Combined with CSS, HTML allows for easy customization of web elements to create visually appealing web applications.
The above is the detailed content of HTML Attributes. For more information, please follow other related articles on the PHP Chinese website!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What are the essential HTML elements for structuring a webpage?
Jul 03, 2025 am 02:34 AM
The web page structure needs to be supported by core HTML elements. 1. The overall structure of the page is composed of , , which is the root element, which stores meta information and displays the content; 2. The content organization relies on title (-), paragraph () and block tags (such as ,) to improve organizational structure and SEO; 3. Navigation is implemented through and implemented, commonly used organizations are linked and supplemented with aria-current attribute to enhance accessibility; 4. Form interaction involves , , and , to ensure the complete user input and submission functions. Proper use of these elements can improve page clarity, maintenance and search engine optimization.
Handling reconnections and errors with HTML5 Server-Sent Events.
Jul 03, 2025 am 02:28 AM
When using HTML5SSE, the methods to deal with reconnection and errors include: 1. Understand the default reconnection mechanism. EventSource retrys 3 seconds after the connection is interrupted by default. You can customize the interval through the retry field; 2. Listen to the error event to deal with connection failure or parsing errors, distinguish error types and execute corresponding logic, such as network problems relying on automatic reconnection, server errors manually delay reconnection, and authentication failure refresh token; 3. Actively control the reconnection logic, such as manually closing and rebuilding the connection, setting the maximum number of retry times, combining navigator.onLine to judge network status to optimize the retry strategy. These measures can improve application stability and user experience.
Declaring the correct HTML5 doctype for modern pages.
Jul 03, 2025 am 02:35 AM
Doctype is a statement that tells the browser which HTML standard to use to parse the page. Modern web pages only need to be written at the beginning of the HTML file. Its function is to ensure that the browser renders the page in standard mode rather than weird mode, and must be located on the first line, with no spaces or comments in front of it; there is only one correct way to write it, and it is not recommended to use old versions or other variants; other such as charset, viewport, etc. should be placed in part.
Implementing client-side form validation using HTML attributes.
Jul 03, 2025 am 02:31 AM
Client-sideformvalidationcanbedonewithoutJavaScriptbyusingHTMLattributes.1)Userequiredtoenforcemandatoryfields.2)ValidateemailsandURLswithtypeattributeslikeemailorurl,orusepatternwithregexforcustomformats.3)Limitvaluesusingmin,max,minlength,andmaxlen
How to group options within a select dropdown using html?
Jul 04, 2025 am 03:16 AM
Use tags in HTML to group options in the drop-down menu. The specific method is to wrap a group of elements and define the group name through the label attribute, such as: 1. Contains options such as apples, bananas, oranges, etc.; 2. Contains options such as carrots, broccoli, etc.; 3. Each is an independent group, and the options within the group are automatically indented. Notes include: ① No nesting is supported; ② The entire group can be disabled through the disabled attribute; ③ The style is restricted and needs to be beautified in combination with CSS or third-party libraries; plug-ins such as Select2 can be used to enhance functions.
Integrating CSS and JavaScript effectively with HTML5 structure.
Jul 12, 2025 am 03:01 AM
HTML5, CSS and JavaScript should be efficiently combined with semantic tags, reasonable loading order and decoupling design. 1. Use HTML5 semantic tags, such as improving structural clarity and maintainability, which is conducive to SEO and barrier-free access; 2. CSS should be placed in, use external files and split by module to avoid inline styles and delayed loading problems; 3. JavaScript is recommended to be introduced in front, and use defer or async to load asynchronously to avoid blocking rendering; 4. Reduce strong dependence between the three, drive behavior through data-* attributes and class name control status, and improve collaboration efficiency through unified naming specifications. These methods can effectively optimize page performance and collaborate with teams.
Implementing Clickable Buttons Using the HTML button Element
Jul 07, 2025 am 02:31 AM
To use HTML button elements to achieve clickable buttons, you must first master its basic usage and common precautions. 1. Create buttons with tags and define behaviors through type attributes (such as button, submit, reset), which is submitted by default; 2. Add interactive functions through JavaScript, which can be written inline or bind event listeners through ID to improve maintenance; 3. Use CSS to customize styles, including background color, border, rounded corners and hover/active status effects to enhance user experience; 4. Pay attention to common problems: make sure that the disabled attribute is not enabled, JS events are correctly bound, layout occlusion, and use the help of developer tools to troubleshoot exceptions. Master this
Submitting Form Data Using New HTML5 Methods (FormData)
Jul 08, 2025 am 02:28 AM
It is more convenient to submit form data using HTML5's FormData API. 1. It can automatically collect form fields with name attribute or manually add data; 2. It supports submission in multipart/form-data format through fetch or XMLHttpRequest, which is suitable for file upload; 3. When processing files, you only need to append the file to FormData and send a request; 4. Note that the same name field will be overwritten, and JSON conversion and no nesting structure need to be handled.
See all articles