jQuery错误:无法使用'在'操作员搜索'使用'在100

jQuery error: Cannot use 'in' operator to search for 'using' in 100

本文关键字:操作员 搜索 使用 错误 jQuery      更新时间:2023-09-26

这段代码曾经工作过,但现在抛出了这个错误,我真的不确定它意味着什么。我在谷歌上搜索了一下,但我发现的似乎很接近,但并不完全符合我的问题。

错误:未捕获的类型错误:无法使用"in"运算符在100 中搜索"using"

JS代码:

    $('a.page-scroll').bind('click', function(event) {
        var $anchor = $(this);
        $('html, body').stop().animate({
            scrollTop: $($anchor.attr('href')).offset(100).top
        }, 1500, 'easeInOutExpo');
        //check to see if its a drop down button
        if(!$(this).hasClass("dropdown"))
        {
            if ( $(window).width() < 767){
                $('.navbar-toggle').click();
            }
        }
       event.preventDefault();
    });

这似乎是造成问题的部分:

   $('html, body').stop().animate({
            scrollTop: $($anchor.attr('href')).offset(100).top
        }, 1500, 'easeInOutExpo');

检查您的$($anchor.attr('href')).offset(100).top值,或者只使用静态数字代替它,如果它正常,那么该值一定有一些错误。

我知道这个问题很古老,但我刚刚遇到了一个类似的问题。如果你查看.offset(coordinates)上的jQuery文档,你会发现它不是在寻找实际的坐标,而是一个包含坐标的对象:

坐标

类型:PlainObject

一个包含属性topleft的对象,这两个属性是指示元素的新顶部和左侧坐标的数字。

所以,如果你这样做:

$('a.page-scroll').bind('click', function(event) {
    var $anchor = $(this);
    var myOffset = { top: "100px", left: "0px" };
    $('html, body').stop().animate({
        scrollTop: $($anchor.attr('href')).offset(myOffset).top
    }, 1500, 'easeInOutExpo');
    //check to see if its a drop down button
    if(!$(this).hasClass("dropdown"))
    {
        if ( $(window).width() < 767){
            $('.navbar-toggle').click();
        }
    }
   event.preventDefault();
});

(注意myOffset变量及其在.offset()调用中的使用)

你应该得到想要的结果!