TypeError: getElementById is null

僤鯓⒐⒋嵵緔 提交于 2019-11-30 10:10:17

问题


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

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

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

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

I found several questions and answers to this.

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"


回答1:


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?

... 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%"/>

It's too late for your code, which finished running long ago.

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




回答2:


It should be document.getElementById('submit').onclick(clicked());




回答3:


your must enclose the id you are searching for in quotes:

document.getElementById('ID_to_look_up');



回答4:


You are executing javascript before your 'body' rendered. Thus document.getElementById("submit") would return null. Because there are no "submit" DOM element yet.

One solution is to move your javascripts under 'body', Or use JQuery with

$(document).ready(function() {
    ...
});

Your variable also has scope problem, your function cannot access variable declared outside this function with 'var' declaration. If you really need that variable, you should remove 'var' declaration. A better way is to move all your variable inside clicked function. like following code

<html>
    <head>
    </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>
<script type="text/javascript">            
            function clicked(){
                var image = document.getElementById("image");
                var desc = document.getElementById("desc");    
                var images = ["http://i.imgur.com/XAgFPiD.jpg", "http://i.imgur.com/XAgFPiE.jpg"];   
                var descs = ["1", "2"];
                var num = 0;
                var total = images.length;     
                num = num + 1;
                if (num > total){
                    num = 0;
                }
                image.src = images[num];
                desc.innerHTML = images[num];
            }
            document.getElementById("submit").onclick = clicked;
        </script>
</html>


来源:https://stackoverflow.com/questions/24972488/typeerror-getelementbyid-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!