【发布时间】:2010-12-13 17:10:31
【问题描述】:
我已经为数组构建了一个原型函数,它不允许我将它与来自 document.forms[0].elements 的数组一起使用。这是显示此问题的简化脚本
<html>
<body>
<form id="frm1" action="form_action.asp">
First name: <input type="text" name="fname" value="Donald" /><br />
Last name: <input type="text" name="lname" value="Duck" /><br />
<input type="submit" value="Submit" />
</form>
<p>Return the value of each element in the form:</p>
<script type="text/javascript">
Array.prototype.returnFirst = function(){
return this[0];
}
alert([1,2,3].returnFirst()); //Why does this work?
alert(document.forms[0].elements.returnFirst().id); //but this one doesn't?
</script>
</body>
</html>
您会看到 returnFirst() 原型函数适用于我的 [1,2,3] 数组,但不适用于我的元素数组?这是怎么回事,还是个数组?
【问题讨论】:
标签: javascript arrays forms prototype