javascript 获取 getElementById 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8084669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting getElementById is Undefined
提问by Cecil Rodriguez
Here's my code:
这是我的代码:
<html>
<header>
<title>Checkup Date</title>
<script type="text/javascript">
function datechange() {
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest(); }
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject
("Microsoft.XMLHTTP");
}
var tr = getElementsById(nameUpdate)
var tds = tr.getElementsByTagName('td');
var user = "";
var date = "";
for(var i = 0, len = tds.length; i < len; ++i) {
user = tds[0];
date = tds[3];
}
var url = "changedate.psp"
var params = "user=" + user + "&date=" + date;
xmlhttp.open("GET", url + "?" + params, true);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
}
</script>
</header>
<body>
<%
Python that doesn't matter
%>
<%= More Python %>
</body>
</html>
My outputted HTML:
我输出的 HTML:
<tr id="TL-D03">
<td>nobody</td>
<td>TL-D03</td>
<td>2010-01-01</td>
<td>
<input type="checkbox" onclick="var nameUpdate = 'TL-D03'; datechange();">
What am I doing wrong here? It says getElementById is undefined.
我在这里做错了什么?它说 getElementById 未定义。
回答by James Hill
getElementById
is a function of document
:
getElementById
是一个函数document
:
var tr = document.getElementById("nameUpdate")
MDN: https://developer.mozilla.org/en/DOM/document.getElementById
MDN:https: //developer.mozilla.org/en/DOM/document.getElementById
回答by Frédéric Hamidi
It's document.getElementById()
, not getElementsById()
, as it only returns one element.
它是document.getElementById()
,不是getElementsById()
,因为它只返回一个元素。
The latter would not be very useful, since id
attributes must be unique within an HTML document.
后者不是很有用,因为id
属性在 HTML 文档中必须是唯一的。
回答by lonesomeday
Problem 1: you're calling getElementsById
. IDs are unique: the function is getElementById
. No s
.
问题 1:您正在调用getElementsById
. ID 是唯一的:函数是getElementById
。没有s
。
Problem 2: getElementById
is not a global function. You need to call it on the document object:
问题2:getElementById
不是全局函数。您需要在文档对象上调用它:
var tr = document.getElementById(nameUpdate)
After all, your script could reference more than one document (for example, with an iframe
), so you need to be explicit about where you expect the element to be.
毕竟,您的脚本可以引用多个文档(例如,带有iframe
),因此您需要明确说明您期望元素的位置。
回答by Dimitri
Also try changing:
也尝试改变:
<input type="checkbox" onclick="var nameUpdate = 'TL-D03'; datechange();">
to this:
对此:
<input type="checkbox" onclick="datechange('TL-D03')">
and
和
function datechange() {
to this
对此
function datechange(nameUpdate) {
makes more sence
更有意义
回答by xgencoder
you need to use document.getElementById("yourelementid")
你需要使用 document.getElementById("yourelementid")
回答by Quentin
The reason it doesn't work is that var
scopes a variable to the function it is defined in, so it isn't readable outside the anonymous function assigned to the onclick handler.
它不起作用的原因是将var
变量范围限定在它定义的函数中,因此它在分配给 onclick 处理程序的匿名函数之外是不可读的。
In more general "wrongness" terms, passing data about using globals is generally a bad idea.
用更一般的“错误”术语来说,传递有关使用全局变量的数据通常是一个坏主意。
Pass it as an argument instead.
将其作为参数传递。
function datechange(nameUpdate) {
and
和
datechange('TL-D03');