html的name和id可以类比身份证的姓名和身份证编号
编号id具有唯一性,一个id只出现一次。
名称name具备可重复性,可以多次出现。
在css中两者都具备识别html元素的作用,name用点号.表示,id用井号#
一般name用于通用多次出现元素的样式定义,id用于唯一性元素样式定义。
在表单当中,由于有些控件具备多元素特性,例如radio checkbox等,使用id不便于表单数据的提交,此外浏览器会根据name来设定发送到服务器的request,因此在表单当中,用name来提交数据。
当然,在实际的html中,也完全可以不用id,用单独的class也可以起到代替id的作用。但是在js中,是无法通过class直接后去html元素的, 定义id便于相关操作。
The name and ID of HTML can be compared to the name and ID number of an ID card.
The ID number is unique and an ID only appears once.
The name is repeatable and can appear multiple times.
In CSS, both have the function of identifying HTML elements. Name is represented by a period. and ID is represented by a pound sign #.
Generally, name is used for the style definition of common elements that appear multiple times, and ID is used for the style definition of unique elements.
In forms, since some controls have multi-element features, such as radio checkbox, etc., using ID is not convenient for submitting form data. In addition, the browser will set the request sent to the server based on the name. Therefore, in the form, name is used to submit data.
Of course, in actual HTML, ID can be completely omitted. A separate class can also replace ID. However, in JS, it is impossible to directly access HTML elements through class. Defining ID facilitates related operations.