HTML中的名称与ID属性 [英] Name vs Id attribute in HTML

查看:96
本文介绍了HTML中的名称与ID属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用< div id =here... 而不是< div name =here有什么好处。 ..



是否都被引用为#here?

解决方案

以下是两者之间的区别:


  1. name从来不是div元素属性。
  2. 在表单控件(input,textarea,select,button元素)上使用的
  3. 名称与名称元素上的id属性完全不同。在这种情况下,name属性与发送给服务器时数据的标签方式有关,多个元素可能共享相同的名称。另一方面,id属性用于识别用于脚本,样式或寻址目的的唯一元素。

  4. 在表单控件之外的其他元素上使用name属性在HTML4.01中与id相同,但它允许使用比id属性更广泛的字符集,并且没有以相同的方式进行控制。由于这种不明确性,W3C决定在这些元素上弃用/删除名称属性,以支持XHTML中的unambigous id属性。但是,这也与XML的另一个细节有关 - 任何元素的只有一个属性可能是类型ID,如果它们让名称保留在元素上,但纠正了歧义问题,则不会是这种情况。


  5. 由于名称属性在这两组元素上的工作方式不同,所以最好删除它们。

  6. 简而言之,为了向后兼容,如果使用HTML4.01,则应该使用name和id属性,它们都设置为相同的值,除了表单控件以外的所有元素,或者XHTML1.0过渡。如果您严格使用XHTML1.0或更高版本,则应仅使用id。对于表单控件,您应该使用名称来表示要将表单发送到服务器和DOM0访问的名称,并且只使用id来进行样式设置,DOM1-3访问或寻址原因。

    Are there any advantages to using <div id="here" ... instead of <div name="here" ...

    are they both referenced to as #here?

    解决方案

    Here are some difference between both :

    1. name has never been a div element attribute.
    2. name as used on the form controls (input, textarea, select, button elements) is radically different from the id attribute on named elements. In this case, the name attribute relates to how data is labeled when sent to server, and multiple elements may share the same name. The id attribute on the other hand is for identifying one unique element for the purposes of scripting, styling, or addressing.
    3. The use of the name attribute on other elements than form controls was in HTML4.01 the same as that of id, but it allowed for a wider set of characters than the id attribute and wasn't controlled in quite the same way. Because of this ambiguity the W3C decided to deprecate/remove name attribute on those elements in favor for the unambigous id attribute in XHTML. This is also connected to another detail of XML however - only one attribute of any element may be of the type ID, which would not be the case if they let name stay on the element but corrected the ambiguity problems.

    4. As the name attribute didn't work the same on those two sets of elements, if was best to remove on of them.

    In short, for backwards compatibility you should use name and id attribute both, both set to the same value, for all elements except form controls if you use HTML4.01 or XHTML1.0 Transitional. If you use XHTML1.0 Strict or later you should use only id. For form controls you should use name for what you want the form to send to the server and for DOM0 access, and only use id for styling, DOM1-3 access or addressing reasons

    这篇关于HTML中的名称与ID属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆