更新:
John Resig也谈到了这个问题。
而这里是一个非常详尽的分析。
本来要是给realazy写的
form 元素内的字段 name 不要跟 form 属性名称一致的comment,但是大概是给出链接太多,被认为是spam的缘故,所以发不出来,只好在这里再贴一次。
这个问题不仅是IE的问题,其他浏览器也是如此。
我提交到mozilla的bug:
https://bugzilla.mozilla.org/show_bug.cgi?id=322488
(我用蹩脚的英文分析了这为什么是一个bug,以及提出了折衷解决方法,不过至今没人睬我 -_-凸,所以这个bug的状态始终是unconfirmed)
全文如下:
DOM level 0 support access form controls via such script:
var f = document.forms[0];
var control1 = f['controlNameOrId'];
var control2 = f.controlNameOrId;
Though DOM HTML spec provide HTMLFormElement.elements to replace this approach,
but MSIE and Firefox still remain this problematic feature.
The issue here is named control may override the attribute or method of
HTMLFormElement. For example, if there is <input name="action" /> in the form,
then document.forms[0].action will return that HTMLInputElement. And there is a
bizarre behavior in firefox that assign still works
document.forms[0].action = 'http://url'
alert(document.forms[0].action == 'http://url')
document.forms[0].submit()
the 1st line is ok in Firefox(but failed in MSIE), but I find no way to
retrieve it back. the 2nd line will alert false, but u still submit to
http://url (if there is no control named 'submit'
Such behavior is undocumented (maybe i missed...), and will confuse developers
if they unfortunately meet some controls named 'method', 'length', 'id',
'item'(IE only), 'target', 'elements'...
To make the script works, especially when you write a reused component or
script library, there should be no such uncontrolled name conflict. But in most
case the web page is written by designer who has no concern about script, so
it's hard to avoid such case. And, even a script master won't remember all
attribute/method of HTMLFormElement.
As a conclusion, form['controlName'] approach should be removed or limited(ie.
won't override the attribute/method of HTMLFormElement).
javaeye也有人曾提及这个问题:
http://yipsilon.iteye.com/blog/136211
更新:
http://anweixiao.iteye.com/blog/86997也描述了所遇到的问题:名为submit的控件会遮蔽form.submit()方法。
这里是CSDN以前的讨论(注意大多数回复都是垃圾,只看我写的就好 -_-# 汗):
http://topic.csdn.net/t/20060118/23/4524484.html
我所知最早提到这个问题的:
http://dulao5.blog.hexun.com/1398177_d.html
最后是我以前写的分析:
http://blog.csdn.net/hax/archive/2006/01/06/571658.aspx
全文如下:
因兼容DOM0的关系,form被设计成支持直接可通过name直接access到control。例如如有一个<input name="user"/>则可直接使用形如document.forms[0]["user"],或在没有特殊字符的情况下,可以直接document.forms[0].user。
然而这造成一个问题,即name若与form上的方法或属性冲突的话(例如form具有length属性,而同时给form增加一个name为length的控件),会如何?
经过我的测试,在IE(XP下的6.0)和FireFox(1.5)中,name都会覆盖原来的属性或方法。也就是优先向前兼容。
作为一种权衡,这种做法本无确定的好坏。但问题是这种行为,在编写脚本中,name与方法冲突下,会产生令developer意想不到的奇怪行为。而且没有在我看到的任何文档中记录。(或许我孤陋寡闻,有方法可以避免?)特别是在IE中,对于Element和一些内置方法的toString()居然都不负责任的返回“[object]”,一旦发生问题,完全令人毫无头绪。
比如,我正好有个脚本要检测form中是否含有特定name的control,而IE的form恰好有个额外的item方法,结果是我必须为此特别处理:
control = myForm[name];
if (typeof control == "undefined" ||
(name == "item" && control == "[object]"))
...
当然,实际上我应该为所有form的属性和方法做特别检测。
显然,为了脚本能正常运行,不应该允许任何name的屏蔽(试想name为action、method、submit、reset等的情况)。然而要网页设计者记住一大堆应为form scripting保留的名字是不现实的。
所以结论是:
这种向前兼容是错误的设计决策。
分享到:
相关推荐
C#窗体控件的属性和方法,帮助我们熟悉窗体的开发,有利于增快开发速度
Form 2.0 控件 Form 2.0 控件
根据扩展属性,方法和事件,你可以把表格设计器控件中的一个表格和菜单封皮传送特色到设计器应用程序中。 Form Designer ActiveX控件 允许使用鼠标和键盘调整大小和重新配置包括的控件。这个功能也支持嵌套...
vb.net 自定义控件 自定义属性 UITypeEditor UI 类型编辑器 实例 提供一个示例 UITypeEditor,它使用 IWindowsFormsEditorService 显示用于用户输入的 Form。 IWindowsFormsEditorService 只能通过 PropertyGrid ...
用户自定义控件 windows form c# 自定义控件 时钟 控件 windows form c# 自定义控件 时钟 控件 windows form c#
一个不错的 Form 多选列表控件效果 - option, multiple, select, form,.files.rar
1.10、水平滚动条控件(HScrollBar)和垂直滚动条控件(VScrollBar)的常用属性 28 1.11、定时器控件的常用属性 30 1.12、文件系统控件的常用属性 30 1.13、形状控件(Shape)常用属性 33 1.14、直线控件(Ling)的常用属性 ...
jquery.form.js解决opera冲突问题 使用jquery.form.js如果jquery使用的是1.9以上的版本会报opera找不到 以解决 备份下
C#常用form控件总结 窗体 1、常用属性 (1)Name属性:用来获取或设置窗体的名称,在应用程序中可通过Name属性来引用窗体。 (2) WindowState属性: 用来获取或设置窗体的窗口状态。 取值有三种: Normal (窗体正常...
1.01、窗体(FORM)的常用属性 属性说明 (Name)窗体的名称 ActiveControl返回焦点所在的控件,该属性设计阶段不可用,运行时只读。 Appearance 外观效果,取值为: 0 平面 1 3D(立体) AutoRedraw 是否自动刷新或重...
一个简单可行的办法用ref.下面是个小例子: 在Form1的button1_Click里写: private void button1_Click(object sender, EventArgs e) { this.button1.Enabled = false; Form2 f2 = new Form2(ref button1);...
在外国的关于 WindowForm 的网站上找到的关于WindowForm 中 控件样式的例子 仿照Vista 控件 样式制造 包含 button ridobutton textbox checkbox 等
c#编程的时候,有时候会用到在当前窗口里面修改了一些参数的时候,希望另外一个窗口控件属性得到刷新更改. 本程序通过简单的代码,实现了此功能,希望能使更多和我一样的C#编程爱好者更好地学会学好C#. 如果您有好的...
通过简单的发送消息,实现在FORM上拖动控件。
c#在多线程中访问Form中控件的多种解决方案,是我看到的最好最全的方案,贡献给大家看看,希望有所启发
form表单,将组件内输入的”switch”,”input”,”checkbox”,”slider”,”radio”,”picker”的值进行提交,数据的格式为:name:value,所以表单中控件都需要添加name属性,否则找不到对应控件的值。其主要属性: ...
.NET form 控件的归纳与整理,对C#初学者很适用
今天小编就为大家分享一篇关于js获取form表单中name属性的值,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
控制Form Caption的控件
DataBindedControls form2 数据绑定到 form1 中的控件 c#语言