The difference between HTML name id and class_PowerNode Java Academy

The difference between HTML name id and class_PowerNode Java Academy

name

Specify a name for the tag.

Format

<input type="text" name="username" />

Application Scenario

①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='the 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.

<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 properties, registering events, etc.

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';
    }
}

characteristic

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

id

Specifies the unique identifier of a tag.

Format

<input type=password id="userpwd" />

Application Scenario

①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.
characteristic

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

class

Specifies the class name of the tag.

Format

<input type=button class="btnsubmit" />

Application Scenario

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

characteristic

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

<<:  The difference between float and position attributes in CSS layout

>>:  Meta declaration annotation steps

Recommend

Vue implements an Input component that gets the key display shortcut key effect

I encountered a requirement to customize shortcut...

Detailed explanation of Vue options

Table of contents 1. What are options? 2. What at...

An example of implementing a simple infinite loop scrolling animation in Vue

This article mainly introduces an example of Vue ...

How to add links to FLASH in HTML and make it compatible with all major browsers

Look at the code first Copy code The code is as fo...

Reasons for the sudden drop in MySQL performance

Sometimes you may encounter a situation where a S...

Detailed explanation of the relationship between React and Redux

Table of contents 1. The relationship between red...

jQuery uses the canvas tag to draw the verification code

The <canvas> element is designed for client...

Specific use of lazy loading and preloading in js

Delayed loading (lazy loading) and preloading are...

The first step in getting started with MySQL database is to create a table

Create a database Right click - Create a new data...

Comparison of storage engines supported by MySQL database

Table of contents Storage Engine Storage engines ...

Docker Tutorial: Using Containers (Simple Example)

If you’re new to Docker, take a look at some of t...

Implementation of vue-nuxt login authentication

Table of contents introduce Link start Continue t...

Six border transition effects implemented by CSS3

Six effectsImplementation Code html <h1>CSS...