使用& quot;""名称"Web开发中输入字段的属性? [英] Is it redundant to use the "name" attribute for input fields in modern web development?

查看:37
本文介绍了使用& quot;""名称"Web开发中输入字段的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到很多网站的表单都包含输入字段,即使它们不是用于样式或脚本编写目的,也已指定了"name"属性!

I've noticed a lot of websites with form(s) containing input fields whose "name" attribute is specified even if it isn't used for styling or scripting purpose!

此外,根据官方文件关于HTML文档中的表单...

Moreover, according to the official document about the form in HTML document ...

名称= cdata [CI] 元素,以便可以引用到样式表或脚本.笔记.此属性已包含在向后兼容.应用领域应该使用id属性识别元素.

name = cdata [CI] This attribute names the element so that it may be referred to from style sheets or scripts. Note. This attribute has been included for backwards compatibility. Applications should use the id attribute to identify elements.

所以,我的问题是:该属性应仅用于样式和脚本编制目的吗?

So, my question is: should this attribute used only for styling and scripting purposes?

提前谢谢!

编辑:特别是,可以避免在文本"类型的输入字段中使用此属性(当没有样式或脚本目的时)?

In particular, could be avoided the use of this attribute with input fields of "text" type (when there aren't no styling or scripting purposes)?

因此,您几乎已经确认了我的想法:在进一步的HTML规范/标准中将不再使用"name"属性!仍然只有代码"才能向后兼容" ...在某些情况下可以避免,但是在某些情况下(例如单选按钮)有必要!

EDIT 2: So, you have almost confirmed what I had thought about: the "name" attribute will be deprecated in further HTML specifications/standards!!!??? It is still "alive" only for backwards compatibility ... in some cases can be avoided but there are still some cases (such as the radio button) where it is necessary!

推荐答案

name 属性是通过非DOM Javascript引用网页范围内特定元素的符号:

The name attribute is the notation to reference specific elements within the scope of a webpage through non-DOM Javascript:

document.forms['your_form'].elements['aa']

该元素的 id 属性需要设置为相同的值,以下各项才能起作用:

The id attribute for the element needs to be set with the same value for the following to work:

document.getElementById('aa')

我的理解是,当Netscape创建Javascript时,它使用了 name 属性.但是,HTML规范决定使用 id ,但保留了 name 以便向后兼容.Internet Explorer 6支持需要使用 name 属性的IME,因为IE中的javascript引擎不会读取 id 属性-仅 name 尽管两者都已定义.

My understanding is that when Netscape created Javascript, it used the name attribute. The HTML spec however decided to go with id, but kept name for backwards compatibility. IME, using the name attribute was required for Internet Explorer 6 support because the javascript engine in IE wouldn't read the id attribute - only the name though both were defined.

...可以避免在文本"类型的输入字段中使用此属性(当没有样式或脚本目的时)?

...could be avoided the use of this attribute with input fields of "text" type (when there aren't no styling or scripting purposes)?

如果您没有在文本字段中附加任何javascript,是的-它们将是不必要的.

If you don't have any javascript attached to the text fields, yes - they would be unnecessary.

这篇关于使用& quot;""名称"Web开发中输入字段的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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