使用“名称”是多余的吗?现代Web开发中的输入字段的属性? [英] Is it redundant to use the "name" attribute for input fields in modern web development?

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

问题描述


$ b $我注意到有很多网站的表单中包含输入字段,它们的name属性被指定,即使它没有用于样式或脚本目的! b

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


name = cdata [CI]
元素,以便它可以从样式表或脚本引用
。注意。
此属性已包含在
向后兼容中。应用程序
应该使用id属性来
标识元素。

所以,我的问题是:should this attribute只用于造型和脚本的目的?

预先感谢!






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





编辑2:所以,你几乎证实了我曾经想过的:名称属性将在更多的HTML规范/标准中被弃用!它仍然是活着的,只有用于向后兼容性 ...在某些情况下可以避免,但仍然有一些情况(例如单选按钮)需要!

解决方案

name 属性是引用通过非DOM Javascript网页的范围:

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

元素的 id 属性需要使用相同的值设置以下功能:

  document.getElementById('aa')

我的理解是当Netscape创建Javascript时,它使用名称属性。 HTML规范决定与 id 一起使用,但为了向后兼容,保留了 name 。使用名称属性的IME对于Internet Explorer 6支持是必需的,因为IE中的JavaScript引擎不会读取 id 属性 - 只有名称,尽管两者都已定义。


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

如果你没有附加任何javascript的文本字段,是的 - 他们将是没有必要的。


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!

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

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?

Thanks in advance!


EDIT: 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)?


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!

解决方案

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']

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

document.getElementById('aa')

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)?

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

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

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