Differences between id and name
The attribute of ID is the highest level. The name level in form is relatively low. If I want to get a value, use these two methods to write it as follows:
This is the name statement.
window.document.form.name.value
And ID, you only need to use
ID. value
You can capture the value directly. Because it is higher than the window level, you only need to use
Both ID and Name can be used to identify a tag. Javascript has two methods, getElementById and getElementByName, respectively, to locate the Dom node.
The differences are as follows:
1. We know that when a Web page is submitted for Post, it is submitted in Form (Form field, A Form contains several Form objects (for example, <input type = "text" name = "UserName" value = "Enter name"/> ), the same page can contain multiple forms (Asp.net, which only supports one Form and must be named Form1). After the Form table is submitted to the server, the value of the form field can be obtained directly through the Name attribute, but the value of the form object cannot be obtained directly through the ID.
2. The same Form cannot have multiple HTML tags with the same name attribute. However, if a webpage has multiple forms, different forms can have tags with the same Name attribute. The ID is global. In an HTML document, multiple nodes cannot use the same ID, no matter which Form it is in.
3. When creating a CSS style, you can create an ID style table (prefix with #) so that the style with this ID can be applied directly, but the Name style table cannot be created.
4. We recommend that you use ID to identify a node during webpage editing, instead of using Name (unless required for Post submission ), we recommend that you use the getElementById method when using Javascript For Dom node locating. Because only the ID can uniquely identify a node, multiple nodes with the same Name attribute may exist in the same webpage, they are in different forms.