The difference between id and name in HTML and js

The difference between id and name in js:

  In js, id and name are used for identification (call of page elements), id is generally unique, but name is not necessarily unique.

  In addition to BASE, HEAD, HTML, META, SCRIPT, STYLE, TITLE tags, id is available.

 

  The name was originally used for identification, but now according to the specification, it is recommended to use id to identify the element. However, name cannot be substituted for the following purposes:

    1. The name of the control of the form, and the submitted data is controlled by the name of the control instead of the id. Because there are many names that correspond to multiple controls at the same time, such as checkbox and radio, and the id must be unique in the entire document. In addition, the browser will set the request sent to the server according to the name. Therefore, if the id is used, the server cannot get the data.

    2. Frame and window names are used to specify targets in other frames or windows.

 

 The following two can be used in common, but it is strongly recommended to use id instead of name:

    Anchor points, usually previously written as <a name="myname">, can now be specified with any element id: <div id = "myid">.

 

The following can only use id:
    1. Association of label and form control,
      <label for = "MyInput"> My Input </ label>
      <input id = "MyInput" type = "text">
      for attribute specifies the element associated with label Id, you cannot substitute name.
    2. The CSS element selection mechanism specifies elements that apply styles in the form of #MyId, and cannot be replaced with name.
    3. Obtain the object
      in the script : IE supports to directly refer to the object identified by the id by id (not name) in the script. For example, the above input, to get the input content in the script, can be directly obtained by MyInput.value.
If you use DOM, use document.getElementById ("MyInput"). Value, if you want to use name, usually get the form containing the control first, such as document.forms [0], and then refer to name from form Is the value that will be sent to the server after calculation.

    Note: The difference between name and id is that id must meet the requirements of identification, such as case sensitivity, it is best not to include underscores (because it is not compatible with CSS). The name basically has no requirements, and even numbers can be used.

Id and name in HTML

 

Reference: https://blog.csdn.net/cindysaj/article/details/83320567

Guess you like

Origin www.cnblogs.com/carrollCN/p/12743488.html