JavaScript错误-document.getElementsById不是函数 [英] Javascript error - document.getElementsById is not a function

查看:68
本文介绍了JavaScript错误-document.getElementsById不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我在循环中调用此代码.但是下面的代码给了我错误,因为 document.getElementsById 不是一个函数.我该怎么办,如何才能循环调用doc.getbyid.

Since I am calling this code in loop. But following code is giving me error as document.getElementsById is not a function. What should I do how can I call doc.getbyid in loop.

for (var z=1; z < i; z++){
       var textbox = document.getElementsById("a"+z).value;
       var textbox2 = document.getElementsById("b").value;
       var textbox3 = document.getElementsById("c").value;
       alert(textbox);
       alert(textbox2);
       alert(textbox3);
}

推荐答案

这是因为它 getElementById (请注意,元素"上缺少"s").如果考虑一下,这是有道理的,因为 id 值在文档中必须是唯一的,因此只有一个匹配的元素",而不是多个元素".

That's because it getElementById (note the lack of the "s" on "Element"). Which makes sense if you think about it, because id values have to be unique in a document, so there will be only one "element" that matches, rather than multiple "elements".

但是,有些方法会返回使用复数元素"的多个元素,例如 getElementsByTagName ,因此您可以将它们混在一起.

However, there are methods that return multiple elements which do use the plural "elements", such as getElementsByTagName, so you may just be mixing them up.

这篇关于JavaScript错误-document.getElementsById不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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