Home > Article > Web Front-end > Xin Xing briefly analyzes the difference between name and id in html_html/css_WEB-ITnose
Maybe novice friends can easily distinguish id and class, but they may not be able to easily distinguish id and name. Let’s talk about the difference and connection between the two.
First is the connection. They can both be used to mark an element and can be operated with JavaScript, but the operation methods are different. GetElementById and getElementByName are used to locate DOM nodes respectively.
Then there is the difference. This is very simple. The first is an outline: name is used for the form, and id is used for the style.
When we submit data in a post, in order to distinguish each element in the form, we use name to distinguish. We cannot obtain the content of each form element through the id.
When we are doing CSS styles, we can create an id style sheet. At this time, with # as the prefix, the style with the id will take effect directly, but the name style sheet cannot be created.
In short, it’s still the same sentence: name is mainly used for forms, and id is mainly used for styles.