Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between the id attribute and the name attribute in html

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you what are the differences between the id attribute and the name attribute in html. I hope you will get something after reading this article. Let's discuss it together.

The id property in html

We use the id attribute to identify a unique HTML element, which can be used as an anchor reference (URL with a # symbol) in URL or as an ID selector in css to style that element. You can also find the element by the value of the id attribute in javascript, using getElementById (), and manipulate the element. Example:

one

two

Test the text! Test the text!

Test the text! Test the text!

one

two

three

Document.getElementById ("p2") .style.color = "red"

The id attribute is generally compatible and is valid for any element. And the value of the id attribute is case-sensitive, and each id value should be unique. Example:

one

two

three

four

Div tag with an id value of a

P label, id value is A

one

two

# a {color: red;}

# A {color: pink;}

Effect picture:

The name property in html

The name attribute is also used to identify the HTML element, but it does not have a unique row, and its value can be reused, for example: radio button

one

two

three

four

five

six

seven

eight

Do you like fruit best?

Apple

Peach

Banana

Pear

Other

Effect picture:

As shown in the example above, the name attribute is often used in forms to submit information; it only works on tag elements such as a, form, iframe, img, map, input, select, textarea, and so on.

The name attribute can be found using getElementsByName () in javascript; it cannot be referenced in CSS or URL. Example:

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

Function getElements ()

{

Var x=document.getElementsByName ("myInput")

Alert (x.length)

}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report