javascript 未捕获的类型错误:无法读取 null 的属性“包含”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32418020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:14:15  来源:igfitidea点击:

Uncaught TypeError: Cannot read property 'contains' of null

javascriptjquery

提问by Exotical

I'm currently working on a project, where I'm using the following slider in the overal site:

我目前正在处理一个项目,我在整个站点中使用以下滑块:

S3Slider

S3滑块

For the particular page I'm currently working on I'm also making use of Walter Zorns' Drag&Drop image library. (Link Here)

对于我目前正在处理的特定页面,我还使用了 Walter Zorns 的拖放图像库。(链接在这里

Now when I start to make use of the SET_DHTMLfunction, which is required for using the D&D library, my slider starts throwing errors:

现在,当我开始使用SET_DHTML使用 D&D 库所需的函数时,我的滑块开始抛出错误:

Uncaught TypeError: Cannot read property 'contains' of null

Uncaught TypeError: Cannot read property 'contains' of null

The line number given, sends me to the following line:

给出的行号将我发送到以下行:

if($(itemsSpan[currNo]).css('bottom') == 0) {

if($(itemsSpan[currNo]).css('bottom') == 0) {

Which lies in the following piece of code:

这在下面的一段代码中:

   s3Slider = function(id, vars) {       

    var element     = this;
    var timeOut     = (vars.timeOut != undefined) ? vars.timeOut : 2000;
    var current     = null;
    var timeOutFn   = null;
    var faderStat   = true;
    var mOver       = false;
    var items       = $("#sliderContent .sliderTopstory");
    var itemsSpan   = $("#sliderContent .sliderTopstory span");

    items.each(function(i) {

        $(items[i]).mouseover(function() {
           mOver = true;
        });

        $(items[i]).mouseout(function() {
            mOver   = false;
            fadeElement(true);
        });

    });

    var fadeElement = function(isMouseOut) {
        var thisTimeOut = (isMouseOut) ? (timeOut/2) : timeOut;
        thisTimeOut = (faderStat) ? 10 : thisTimeOut;
        if(items.length > 0) {
            timeOutFn = setTimeout(makeSlider, thisTimeOut);
        } else {
            console.log("Poof..");
        }
    }

    var makeSlider = function() {
        current = (current != null) ? current : items[(items.length-1)];
        var currNo      = jQuery.inArray(current, items) + 1
        currNo = (currNo == items.length) ? 0 : (currNo - 1);
        var newMargin   = $(element).width() * currNo;
        if(faderStat == true) {
            if(!mOver) {
                $(items[currNo]).fadeIn((timeOut/6), function() {
                 /* This line ->   */if($(itemsSpan[currNo]).css('bottom') == 0) {
                        $(itemsSpan[currNo]).slideUp((timeOut/6), function( ) {
                            faderStat = false;
                            current = items[currNo];
                            if(!mOver) {
                                fadeElement(false);
                            }
                        });
                    } else {
                        $(itemsSpan[currNo]).slideDown((timeOut/6), function() {
                            faderStat = false;
                            current = items[currNo];
                            if(!mOver) {
                                fadeElement(false);
                            }
                        });
                    }
                });
            }
        } else {
            if(!mOver) {
                if($(itemsSpan[currNo]).css('bottom') == 0) {
                    $(itemsSpan[currNo]).slideDown((timeOut/6), function() {
                        $(items[currNo]).fadeOut((timeOut/6), function() {
                            faderStat = true;
                            current = items[(currNo+1)];
                            if(!mOver) {
                                fadeElement(false);
                            }
                        });
                    });
                } else {
                    $(itemsSpan[currNo]).slideUp((timeOut/6), function() {
                    $(items[currNo]).fadeOut((timeOut/6), function() {
                            faderStat = true;
                            current = items[(currNo+1)];
                            if(!mOver) {
                                fadeElement(false);
                            }
                        });
                    });
                }
            }
        }
    }

    makeSlider();

};  

Why is this error being thrown?

为什么会抛出这个错误?

Thanks.

谢谢。

采纳答案by Mike S.

I think your problem lies in these two lines:

我认为你的问题在于这两行:

var currNo = jQuery.inArray(current, items) + 1
currNo = (currNo == items.length) ? 0 : (currNo - 1);

If jQuery doesn't find the item in the array, it's going to send you a value of -1 (on the top line) which will then be changes to 0 because you added 1. Now, if currNo is 0 on the second line, it's going to change it back to -1, which will return you undefined. Maybe try and change it to do this instead:

如果 jQuery 没有在数组中找到该项目,它将向您发送一个 -1(在第一行)的值,然后由于您添加了 1,该值将更改为 0。现在,如果第二行的 currNo 为 0 ,它将把它改回 -1,这将返回 undefined。也许尝试更改它以执行此操作:

var currNo = jQuery.inArray(current, items);
if (currNo === items.length - 1) {
    currNo = 0;
}

I'm not positive this is the problem, but I can see this becoming an issue if it's not the problem you're currently having.

我不确定这是问题所在,但如果这不是您目前遇到的问题,我可以看到这会成为一个问题。