开发者问题收集

未捕获的类型错误:无法在 an.html:895 处设置 null 的属性“innerHTML”

2017-08-07
515

我希望有人能帮助我,

在谷歌浏览器控制台中,显示的错误是

Uncaught TypeError: Cannot set property 'innerHTML' of null at examples.html:895

but this error is looping Up to thousands of times, The code I put before </body>

这是我的代码

 function dezText(words, id) {
    'use strict';
    var visible = true;
    var letterCount = 1;
    var x = 1;
    var waiting = false;
    var target = document.getElementById(id);
    window.setInterval(function() {

        if (letterCount === 0 && waiting === false) {
            waiting = true;
            target.innerHTML = words[0].substring(0, letterCount);
            window.setTimeout(function() {
                var usedWord = words.shift();
                words.push(usedWord);
                x = 1;
                letterCount += x;
                waiting = false;
            }, 500)
        } else if (letterCount === words[0].length + 1 && waiting === false) {
            waiting = true;
            window.setTimeout(function() {
                x = -1;
                letterCount += x;
                waiting = false;
            }, 1000)
        } else if (waiting === false) {
            target.innerHTML = words[0].substring(0, letterCount);
            letterCount += x;
        }
    }, 70)
}
2个回答

id 在您的函数中发送 null 为了删除控制台中的错误,您可以在函数中设置条件,检查“id”是否为 null 或有值,如果 id 有值,则运行其他代码

     function dezText(words, id) {
        'use strict';
if(id){
        var visible = true;
        var letterCount = 1;
        var x = 1;
        var waiting = false;
        var target = document.getElementById(id);
        window.setInterval(function() {

            if (letterCount === 0 && waiting === false) {
                waiting = true;
                target.innerHTML = words[0].substring(0, letterCount);
                window.setTimeout(function() {
                    var usedWord = words.shift();
                    words.push(usedWord);
                    x = 1;
                    letterCount += x;
                    waiting = false;
                }, 500)
            } else if (letterCount === words[0].length + 1 && waiting === false) {
                waiting = true;
                window.setTimeout(function() {
                    x = -1;
                    letterCount += x;
                    waiting = false;
                }, 1000)
            } else if (waiting === false) {
                target.innerHTML = words[0].substring(0, letterCount);
                letterCount += x;
            }
        }, 70)
    }
    }
Abbas Hasani
2017-08-07

My Problem is solved

thanks everybody, thanks stacnk overvlow.

非常感谢,老兄,但是我使用了我的代码,它对我有用,我的问题解决了

 function dezText(words, id) {
    'use strict';
if($('#'+id).length > 0)
{
    var visible = true;
    var letterCount = 1;
    var x = 1;
    var waiting = false;
    var target = document.getElementById(id);
    window.setInterval(function() {

        if (letterCount === 0 && waiting === false) {
            waiting = true;
            target.innerHTML = words[0].substring(0, letterCount);
            window.setTimeout(function() {
                var usedWord = words.shift();
                words.push(usedWord);
                x = 1;
                letterCount += x;
                waiting = false;
            }, 500)
        } else if (letterCount === words[0].length + 1 && waiting === false) {
            waiting = true;
            window.setTimeout(function() {
                x = -1;
                letterCount += x;
                waiting = false;
            }, 1000)
        } else if (waiting === false) {
            target.innerHTML = words[0].substring(0, letterCount);
            letterCount += x;
        }
    }, 70)
}   

>

Nando M. A
2017-08-07