TypeError:getElementById为null [英] TypeError: getElementById is null

查看:80
本文介绍了TypeError:getElementById为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<html>
    <head>
        <script type="text/javascript">
            var image = document.getElementById(image);
            var desc = document.getElementById(desc);
            var images = ["http://i.imgur.com/XAgFPiD.jpg", "http://i.imgur.com/XAgFPiD.jpg"]
            var descs = ["1", "2"]
            var num = 0;
            var total = images.length;
            function clicked(){
                 num = num + 1;
                 if (num > total){
                    num = 0;
                 }
                 image.src = images[num];
                 desc.innerHTML = images[num];
            }
            document.getElementById(submit).onclick(clicked());
        </script>
    </head>
<body>
    <div><h2>Project |</h2><h2> | herbykit</h2></div>
    <div>
        <button id="submit">Next</button><br/>
        <img id="image" src="http://i.imgur.com/XAgFPiD.jpg" height="20%" width="50%"/>
        <p id="desc">first desc.</p>
   </div>
</body>
</html>

document.getElementById(submit).onclick(clicked());行抛出错误
ReferenceError:未定义提交

The line "document.getElementById(submit).onclick(clicked());" throws an error "ReferenceError: submit is not defined"

当我尝试访问常规按钮时

[通过getElementsByClassName& getElementsByTagName]

,它给出了错误 ReferenceError:未定义按钮。

When I tried accessing buttons in general
[through getElementsByClassName & getElementsByTagName]
it gave an error of "ReferenceError: button is not defined"

在getElementById中使用字符串会引发错误 getElementById为null

Using strings in getElementById it throws the error "getElementById is null"

我发现了几个问题和答案。

I found several questions and answers to this.

我只有一个人知道如何实现,由于使用了PHP,而这在大多数其他情况下都是错误。我发现的其他解决方案在数字上涉及错误。
针对此错误,我尝试修复了printwindow.document.getElementById(.. etc
。这给了我一个错误 ReferenceError:未定义printwindow。

Only one of them I understood how to implement, due to the use of PHP and that being the error on most others. Other solutions I found involved errors numerically. On this error I tried a fix of printwindow.document.getElementById(..etc This gives me an error of "ReferenceError: printwindow is not defined"

推荐答案

浏览器会尽快运行JavaScript以加快渲染速度,因此当您收到以下代码时:

Browsers run JavaScript as soon as possible in order to speed up rendering. So when you receive this code:

<html>
    <head>
        <script type="text/javascript">
            var image = document.getElementById(image); // Missing quotes, typo?

...在中间运行。没有< foo id = image> 在页面上,所以您得到 null 。最后,您将获得页面的其余部分呈现的内容,包括:

... in runs intermediately. There's no <foo id="image"> on page yet, so you get null. Finally, you get the rest of the page rendered, including:

<img id="image" src="http://i.imgur.com/XAgFPiD.jpg" height="20%" width="50%"/>

为时已晚

您需要绑定一个window.onload甚至处理程序并运行您的代码DOM准备就绪时(或将所有JavaScript移到图片底部的页面底部)编写代码。

You need to bind a window.onload even handler and run your code when the DOM is ready (or move all JavaScript to page bottom, after the picture).

这篇关于TypeError:getElementById为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆