所有版本的IE返回SCRIPT5007无法获取属性'长度'的未定义引用或null引用

All versions of IE return SCRIPT5007 Unable to get property 'length' of undefined or null reference

本文关键字:引用 null 长度 未定义 属性 获取 IE 版本 返回 SCRIPT5007      更新时间:2023-09-26

我有以下代码:

$('.sublime-typer').each(function() {
    var maximum = $(this).attr('data-terminal');
    for (var j = 1; j <= maximum; j++) {
        var $el = $('.sublime-typer[data-terminal="' + j + '"]'),
            txt = $el.html(),
            txtLen = txt.length,
            timeOut,
            char = 0,
            drawValue = 1;
        $el.text('|');
        (function typeIt() {
            var humanize = Math.round(Math.random() * (2 - 50)) + 30;
            timeOut = setTimeout(function() {
                char++;
                var type = txt.substring(0, char);
                if (type.indexOf('data-compiled="true-' + drawValue + '"') >= 0) {
                    var currentClass = $('.drawValue-' + drawValue).attr('class');
                    $('.drawValue-' + drawValue).attr('class', currentClass + ' activated');
                    $('.drawValue-' + drawValue).addClass('activated');
                    // For firefox as it doesn't support svg letter spacing :(
                    if( (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) && (drawValue == 2) ){
                        $('.drawValue-' + drawValue).attr('dx', '0 -3px')
                    }
                    drawValue = drawValue + 1;
                }
                $el.html(type + '|');
                typeIt();
                if (char == txtLen) {
                    $el.html($el.html().slice(0, -1)); // removes the '|'
                    clearTimeout(timeOut);
                }
            }, humanize);
        }());
    }
});

它在所有版本的Firefox、Chrome和Safari中都能完美运行,但在任何版本的IE上,它都会返回错误:SCRIPT5007: Unable to get property 'length' of undefined or null reference.如果我在脚本周围有window.load或document.ready,并且如果我将txtLen = txt.length,更改为txtLen = txt.val,,但错误中使用val而不是长度,则会得到相同的错误,这并没有什么区别。

我看了很多其他问题,都有同样的SCRIPT5007问题,但我看的都不起作用。非常感谢在这方面的任何帮助。

编辑:在此创建一个Fiddle

一个或多个.sublime-typer元素缺少数据属性data-terminal

尝试

txt = $el.text(),
txtLen = txt.length,
//etc

它看起来像SVG+IE=没有乐趣。

您遇到的基本问题是.html()在任何情况下都会返回"undefined"。

将".html()"的实例更改为".prop('textContent')",并从".html(newval)"更改为".prop('extContent',newval))"。

如果你做出了这些改变,那么你只需要解决换行问题。也许您应该将一些字符放在''r''n想要中断的位置,textContent会保留它们(因为它通常会去掉tSpan)。