我运行了下面的代码,发现了一个奇怪的问题:图像输入按钮没有被视为子元素表单?谁能告诉我为什么会发生这种情况?
window.onload = function(event) {
formElements = document.forms[0].elements;
//output 2
console.log(formElements.length);
//output only two elements
for (var i = 0; i < formElements.length; i++) {
console.log(formElements[i]);
}
};
<form id="form" name="form" action="" method="post">
<input type="text" name="username" value="" />
<button>Submit</button>
<input type="image" src="http://www.starmarkcabinetry.com/Style%20Library/StarMark/images/right-button-small.png" />
</form>
最佳答案
The HTMLFormElement.elements property returns an HTMLFormControlsCollection (HTML 4 HTMLCollection) of all the form controls contained in the FORM element, with the exception of input elements which have a type attribute of image.
MDN 文档 HTMLFormElement.elements
与Listed Items相关的规范
您可以使用children
来获得相同的结果。
我找不到这样做的原因,我对输入感觉不好[type=image]我希望与 Web APIS 密切合作的人可以阐明一些有趣的原因,例如与冥王星发生的事情
历史原因可查here
关于javascript - 为什么 JavaScript 中图像输入按钮不被视为表单的子元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43153357/