隐藏的 javascript getElementById 问题
我正在尝试制作一个书签,它将返回我注册的网站的用户 ID。登录网站后,有一段代码如下:
<input id="memberID" name="00V85345345343ugFFC" type="hidden" value="3959721"/>
这是我正在使用的 JavaScript,但它不执行任何操作。我在这里做错了什么吗?
javascript:alert("Your member ID is\n"+document.getElementById("memberID")[0].value);
I am trying to make a bookmarklet that will return a user id for a website i am registered for. When logged into the website there is a piece of code as follows:
<input id="memberID" name="00V85345345343ugFFC" type="hidden" value="3959721"/>
This is the JavaScript i am using but it doesnt do anything. AmI doing something wrong here?
javascript:alert("Your member ID is\n"+document.getElementById("memberID")[0].value);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
document.getElementById
返回HTMLElement
或null
(如果未找到与该 ID 匹配的元素)。因为元素 ID 应该是唯一的,所以返回 Array 是没有意义的;最多应匹配一个元素。
然而,例如,
document.getElementsByTagName
(注意元素的复数,而不是元素)返回一个元素数组;因为在一页中拥有多个相同标签的元素是非常有意义的。document.getElementById
returns aHTMLElement
ornull
if no element was found matching that ID.Because element ID's are supposed to be unique, it makes no sense to return an Array; a maximum of one element should be matched.
document.getElementsByTagName
however, for example (note the plural of elements, rather than element), returns an array of elements; as it makes perfect sense to have more than one element of the same tag in a page.getElementById 返回单个元素,而不是元素数组。
所以这:
必须变成:
getElementById returns single element, not array of elements.
So this:
Must become just:
也许这将有助于清除该错误:
http ://p-xr.com/jquery-like-getelementbyid-in-1-line-of-code/
Maby this will help abit in cleaning up that error:
http://p-xr.com/jquery-like-getelementbyid-in-1-line-of-code/