getElementById() 始终显示 NULL 状态
我正在学习有关事件处理程序的 JavaScript 课程,同时我开始学习非常基础的内容,即 getElementById()
。下面是我的 HTML 和 JavaScript 代码。下面的代码也有图像标签,但我是 SO 的新用户,这就是为什么他们不允许我以确切的方式发布它们。因此,请考虑将 src
标记完全兼容。
<div class="considerVisiting">
<img src="images/bq-twitter" id="mainImage" alt="BeQRious Twitter">
</div>
这是 JavaScript 代码:
var myImage = document.getElementById("mainImage");
var imageArray = [images/1-btn, images/download-as-you-like];
var imageIndex = 0;
function changeImage() {
myImage.setAttribute("src",imageArray[imageIndex]);
imageIndex++;
if (imageIndex >= imageArray.length) {
imageIndex=0;
}
}
setInterval(changeImage, 5000);
该代码将每 5 秒更改一次 mainImage 图片。但我面临的问题是,当我在 Mozilla 中使用 FireBug 运行此脚本时,它会显示:
> Error : myImage is Null
并且该脚本没有任何反应。
I am going through lessons of JavaScript about Event Handlers, while doing so I am starting very basic which is getElementById()
. Below is my HTML and JavaScript Code. The following code has images tag also but I am new user to SO that's why they didn't allowed me to post them in the exact way. So consider src
tags to be fully complaint.
<div class="considerVisiting">
<img src="images/bq-twitter" id="mainImage" alt="BeQRious Twitter">
</div>
and here is the JavaScript Code:
var myImage = document.getElementById("mainImage");
var imageArray = [images/1-btn, images/download-as-you-like];
var imageIndex = 0;
function changeImage() {
myImage.setAttribute("src",imageArray[imageIndex]);
imageIndex++;
if (imageIndex >= imageArray.length) {
imageIndex=0;
}
}
setInterval(changeImage, 5000);
This code will change the mainImage Picture every 5 seconds. But the problem I am facing is that when I run this script using FireBug in Mozilla it says:
> Error : myImage is Null
and nothing happens with regard to the script.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
更改:
至:
** 无效。
Change:
To:
The ** are not valid.
如果不存在指定 id 的元素,
.getElementById()
应该返回 null,因此如果脚本在解析相关元素之前运行,您所看到的行为是正确的。只是猜测,但您的脚本是否位于文档的中?脚本(包括头部)在浏览器从上到下解析文档时遇到它时运行。您的脚本只能访问浏览器已解析的元素,即文档中较高/较早的元素,因此如果您将
部分移至正文底部,它将(显然)在您的所有元素之后并且能够访问它们。
另一种方法是在文档的“onload”或“document.ready”上运行代码。 “onload”是浏览器仅在整个文档加载后才会执行的事件。
唯一明显错误的另一件事是这一行:
我认为这应该是一个字符串数组,所以应该是这样的:
.getElementById()
is supposed to return null if no element of the specified id exists, so the behaviour you are seeing is correct if your script is running before the element in question is parsed. Just a guess, but is your script in the<head>
of your document? Script (including in the head) runs as the browser encounters it while it is parsing the document from top to bottom. Your script can only access elements that have already been parsed by the browser, i.e., elements higher/earlier in the document, so if you move your<script>
section to the bottom of the body it will (obviously) be after all your elements and will be able to access them.The other way to do it is to run your code "onload" or on "document.ready" of the document. "onload" is an event that the browser will execute only after the whole document has been loaded.
The only other thing that jumps out as obviously wrong is this line:
I assume this was meant to be an array of strings, and so should be like this:
在您的代码中,数组中的值应该是字符串文字,即
应该是:
另外,除非有充分的理由,否则不要使用“setAttribute”。请使用 DOM 属性,因为“setAttribute”在某些情况下存在问题且不可靠。读起来也更自然,因为您实际上是在尝试设置 DOM 对象的属性,而不是 HTML 标记的属性,因此:
应该是:
另外:
可以使用模运算符进行管理 %:
整个函数可以减少to:
你也可以通过使用闭包来摆脱全局变量:
但是,你必须在 DOM 中存在 id mainImage 的元素之后运行上面的代码(例如,你无法从头运行它)。最好使用 window.onload 调用该函数并传递 id。
In your code, the values in the array should be string literals, i.e.
should be:
Also, don't use 'setAttribute' unless you have a good reason. Use DOM properties instead as 'setAttribute' is buggy and unreliable in some cases. It is also more natural to read as you are really trying to set a property of a DOM object, not an attribute of HTML markup, so:
should be:
Also:
can be managed using the modulus operator %:
The whole function can be reduced to:
You can also get rid of the global variables by using closures:
However, you must run the above code after the element with id mainImage exists in the DOM (e.g. you can't run it from the head). It is likely better to call the function and pass the id using window.onload.