getElementById() 始终显示 NULL 状态

发布于 2024-12-21 04:38:48 字数 885 浏览 2 评论 0原文

我正在学习有关事件处理程序的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

更改:

<img src="images/bq-twitter" **id="mainImage"** alt="BeQRious Twitter" /> 

至:

<img src="images/bq-twitter" id="mainImage" alt="BeQRious Twitter" /> 

** 无效。

Change:

<img src="images/bq-twitter" **id="mainImage"** alt="BeQRious Twitter" /> 

To:

<img src="images/bq-twitter" id="mainImage" alt="BeQRious Twitter" /> 

The ** are not valid.

梅窗月明清似水 2024-12-28 04:38:48

如果不存在指定 id 的元素,.getElementById() 应该返回 null,因此如果脚本在解析相关元素之前运行,您所看到的行为是正确的。只是猜测,但您的脚本是否位于文档的 中?脚本(包括头部)在浏览器从上到下解析文档时遇到它时运行。您的脚本只能访问浏览器已解析的元素,即文档中较高/较早的元素,因此如果您将

另一种方法是在文档的“onload”或“document.ready”上运行代码。 “onload”是浏览器仅在整个文档加载后才会执行的事件。

唯一明显错误的另一件事是这一行:

var imageArray= [images/1-btn,images/download-as-you-like];

我认为这应该是一个字符串数组,所以应该是这样的:

var imageArray= ["images/1-btn", "images/download-as-you-like"];

.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:

var imageArray= [images/1-btn,images/download-as-you-like];

I assume this was meant to be an array of strings, and so should be like this:

var imageArray= ["images/1-btn", "images/download-as-you-like"];
独夜无伴 2024-12-28 04:38:48

在您的代码中,数组中的值应该是字符串文字,即

> var imageArray = [images/1-btn, images/download-as-you-like];

应该是:

var imageArray = ['images/1-btn', 'images/download-as-you-like'];

另外,除非有充分的理由,否则不要使用“setAttribute”。请使用 DOM 属性,因为“setAttribute”在某些情况下存在问题且不可靠。读起来也更自然,因为您实际上是在尝试设置 DOM 对象的属性,而不是 HTML 标记的属性,因此:

> myImage.setAttribute("src",imageArray[imageIndex]);

应该是:

myImage.src = imageArray[imageIndex];

另外:

> if (imageIndex >= imageArray.length) {
>   imageIndex=0;
> }

可以使用模运算符进行管理 %:

  imageIndex = imageIndex % imageArray.length;

整个函数可以减少to:

function changeImage() {
  myImage.src = imageArray[index++ % imageArray.length];
}

你也可以通过使用闭包来摆脱全局变量:

var changeImage = (function() {

    var myImage = document.getElementById("mainImage");
    var imageArray = ['images/1-btn', 'images/download-as-you-like'];
    var imageIndex = 0;

    return function () {
        myImage.src = imageArray[index++ % imageArray.length];
    }
}());

但是,你必须在 DOM 中存在 id mainImage 的元素之后运行上面的代码(例如,你无法从头运行它)。最好使用 window.onload 调用该函数并传递 id。

In your code, the values in the array should be string literals, i.e.

> var imageArray = [images/1-btn, images/download-as-you-like];

should be:

var imageArray = ['images/1-btn', 'images/download-as-you-like'];

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:

> myImage.setAttribute("src",imageArray[imageIndex]);

should be:

myImage.src = imageArray[imageIndex];

Also:

> if (imageIndex >= imageArray.length) {
>   imageIndex=0;
> }

can be managed using the modulus operator %:

  imageIndex = imageIndex % imageArray.length;

The whole function can be reduced to:

function changeImage() {
  myImage.src = imageArray[index++ % imageArray.length];
}

You can also get rid of the global variables by using closures:

var changeImage = (function() {

    var myImage = document.getElementById("mainImage");
    var imageArray = ['images/1-btn', 'images/download-as-you-like'];
    var imageIndex = 0;

    return function () {
        myImage.src = imageArray[index++ % imageArray.length];
    }
}());

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文