Introduction to the differences between HTML name, id, class (format/application scenario/features), etc.

Introduction to the differences between HTML name, id, class (format/application scenario/features), etc.
In a page, there are many controls (elements or tags). In order to operate these tags more conveniently, they need to be marked with an identity card.

The attributes required are: name, id, class

1. name

Specify a name for the tag.

1.1 Format: <input type="text" name="username" />

1.2 Application Scenarios

①form: name can be used as the variable name of the form list transferred to the server; for example, the name transferred to the server above is: username='value of text';

②Input type='radio' radio tag: When the name of several radio tags is set to the same value, a radio selection operation will be performed.

Copy code
The code is as follows:

<input type="radio" name='sex'/>Male
<input type="radio" name='sex'/>Female③Quickly obtain a group of tags with the same name: obtain tags with the same name and perform operations together, such as: changing attributes, registering events, etc.;
[code]
function changtxtcolor() {
var txts = document.getElementsByName('txtcolor'); //Get all tags with name=txtcolor
for (var i = 0; i < txts.length; i++) { //Loop through the tags and change the background color to red
txts[i].style.backgroundColor = 'red';
}
}

1.3 Features

The value of the name attribute is not unique in the current page.

2. id

Specifies the unique identifier of a tag.

2.1 Format: <input type=password id="userpwd" />

2.2 Application scenarios:

①Quickly obtain the tag object based on the unique ID number provided. For example: document.getElementById(id)

② Used as the value of the for attribute of the label tag: Example: <label for='userid'>Username: </label>, which means that when this label tag is clicked, the label with id userid gets the focus.

2.3 Features

The value of the id attribute must be unique on the current page.

3. class

Specifies the class name of the tag.

3.1 Format: <input type=button class="btnsubmit" />

3.2 Application scenarios:

①CSS operation, put some specific styles into a class class, and add this class if you need tags of this style.

3.3 Features:

You can put multiple classes in one class attribute, but they must be separated by spaces; for example: class='btnsubmit btnopen'

<<:  MySQL data aggregation and grouping

>>:  Detailed steps to expand LVM disk in Linux

Recommend

Solution to the conflict between two tabs navigation in HTML

Let's start with a description of the problem...

Database SQL statement optimization

Why optimize: With the launch of the actual proje...

CocosCreator ScrollView optimization series: frame loading

Table of contents 1. Introduction 2. Analysis of ...

Nginx Layer 4 Load Balancing Configuration Guide

1. Introduction to Layer 4 Load Balancing What is...

Commonly used HTML format tags_Powernode Java Academy

1. Title HTML defines six <h> tags: <h1&...

A Different Kind of "Cancel" Button

The “Cancel” button is not part of the necessary ...

How to create a web wireframe using Photoshop

This post introduces a set of free Photoshop wire...

Docker packages the local image and restores it to other machines

1. Use docker images to view all the image files ...

Detailed code examples of seven methods for vertical centering with CSS

When we edit a layout, we usually use horizontal ...

How to view image information in Docker

In this article, we will need to learn how to vie...

VUE realizes registration and login effects

This article example shares the specific code of ...