Javascript:document.getElementById()返回NULL [英] Javascript: document.getElementById() returns NULL
问题描述
我对Javascript很新,我遇到了一个问题,即document.getElementById()总是返回NULL,这让我疯狂。
I am quite new with Javascript and I got a problem with document.getElementById() that always returns NULL, and that's driving me nuts.
我有一个元素在我的代码中,我想得到它的坐标,所以我可以移动它。
I have a element in my code and I want to get its coordinates so I can move it.
这是代码:
<html>
<head>
<script type="text/javascript" >
function MoveIt(obj) {
alert(obj); // returns "Object HTMLDivElement"
var xx = document.getElementById("arect");
if(document.getElementById("arect").value == null) {
alert('NULL >> ' + xx.value);
}
else {
alert('NOT NULL >>' + xx.value);
}
posX = xx.style.scrollTop;
posY = xx.style.left;
}
</script>
</head>
<body bgcolor="white" >
<DIV class="background" id="MyDiv2">
<div id="arect" name="arect" class="transbox" onmousedown="MoveIt(this);" >
</div>
</div>
</body>
</html>
上述函数MoveIt()始终返回NULL
The above function MoveIt() always returns NULL
推荐答案
您从未检查 getElementById(...)
NULL
。
您检查 getElementById(...)。value
for NULL
,div没有值。
You checked getElementById(...).value
for NULL
, and divs don't have a "value".
另请注意,您忘记关闭< div /> ;
标记,在您的XHTML中是非法的...并且由于某种原因使用了SVG doctype。 SVG不是HTML。
Also note that you forgot to close that <div />
tag, which is illegal in your XHTML... and used an SVG doctype for some reason. SVG is not HTML.
你在这里尝试做什么并不是很清楚。
It's not really clear what you're trying to do here.
这篇关于Javascript:document.getElementById()返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!