在document.forms [x]中删除DOM中的图像输入是否正常? [英] is it normal for image inputs to be omitted from the DOM in document.forms[x].elements?

查看:99
本文介绍了在document.forms [x]中删除DOM中的图像输入是否正常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现在HTML页面中给出了一个这样的表单:

I've found that given a form in a HTML page like this:

<form name="form">
   <input type="image" name="foo" 
          src="somewhere.gif" alt="image" value="blah"/>
   <input type="text" name="bar" value="blah"/>
</form>

在Javascript中通过DOM访问元素时,有元素为图像输入!只是省略了因此, document.forms [0] .elements.length 是1,而 document.forms [0] .element [0] .type 是text。

When accessing the elements via the DOM in Javascript, there is no element for the image input! It is just omitted. So, document.forms[0].elements.length is 1, and document.forms[0].element[0].type is "text".

在Firefox和IE中似乎是这样。我在参考书或网络上的任何地方都找不到这个事实。我可以找到的只是一个不错的评论:

This seems to be the case in Firefox, and IE. I can't find this fact documented anywhere in my reference books or on the web. All I can find is a throwaway comment here:

https://bugzilla.mozilla.org/show_bug.cgi?id=163822#c4

哪个建议它只是就是这样。如果是这样,那么就这样 - 但是它真的没有在任何地方记录?这是一个历史错误,还是有原因?

Which suggests it "just is like this". If so, well so be it - but is it really not documented anywhere? Is it a historical mistake, or is there a reason for it?

推荐答案

看起来这是元素所有浏览器中的属性。

It looks like that's the behavior of the elements property in all browsers.

但是,您仍然可以通过JavaScript中的DOM访问它,使用 childNodes 属性。

However, you should still be able to access it through the DOM in JavaScript using the childNodes property.

对于您的示例:

document.forms[0].childNodes.length; // equals 5 (2 inputs and 3 text nodes).
document.forms[0].childNodes[1];     // This is your input with type='image'

这篇关于在document.forms [x]中删除DOM中的图像输入是否正常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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