jQuery 未捕获的语法错误,无法识别的表达式:[object Object]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7469925/
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-08-26 23:53:47  来源:igfitidea点击:

Uncaught Syntax error, unrecognized expression: [object Object]

jquery

提问by Iladarsda

Working currenly on news scroller - see my live example here - EXAMPLE

目前在新闻滚动条上工作 - 在这里查看我的现场示例 -示例

When I press next/prev arrow I'm getting an error log Uncaught Syntax error, unrecognized expression: [object Object]

当我按下一个/上一个箭头时,我收到错误日志 Uncaught Syntax error, unrecognized expression: [object Object]

Why is the problem? Where is the error in the syntax?

为什么会出现问题?语法错误在哪里?

jQuery Code:

jQuery代码:

        (function($) {
    /*!  Scroller
        ---------------------------------------------*/
        $.fn.Scroller = function() {

            //Set height
            $('.scroller').each(function() {
                var height = 0;
                $(this).children('div').each(function() {

                    if (height < $(this).height()) {
                        height = $(this).height();
                    }

                });
                $(this).css("height", height + "px");

            });

            $('.scroller').each(function() {

                var NextArrow = $(this).parent().find('.next');
                var PrevArrow = $(this).parent().find('.prev');


                // Set a timeout
                var timeOut = setTimeout(nextNotice, 5000);

                // pause on hover
                $(this).hover(

                function() {
                    clearTimeout(timeOut);
                }, function() {
                    timeOut = setTimeout(nextNotice, 5000);
                });

                // Next notice function called on timeout or click
                //set a flag that use to be an oberver to listen when the fadeIn done
                var flag = true;

                function nextNotice(event) {

                    var CurrentScrollerDiv = $(this).parent().find('.scroller');

                    if (!flag) {
                        return false;
                    }
                    clearTimeout(timeOut);

                    flag = false;
                    timeOut = setTimeout(nextNotice, 5000);

                    if ($(CurrentScrollerDiv + ' div:visible').is(CurrentScrollerDiv + '  div:last-child')) {
                        $(CurrentScrollerDiv + ' div:visible').fadeOut(300);
                        $(CurrentScrollerDiv + ' div:first-child').fadeIn("slow", function() {
                            flag = true;
                        });
                    } else {
                        $(CurrentScrollerDiv + ' div:visible').fadeOut(300).next('div').fadeIn("slow", function() {
                            flag = true;
                        });
                    }
                    return false;
                }

                $(NextArrow).click(nextNotice);
                $(PrevArrow).click(function(event) {

                    var CurrentScrollerDiv = $(this).parent().find('.scroller');

                    if (flag) {
                        return false;
                    }
                    clearTimeout(timeOut);

                    flag = false;
                    timeOut = setTimeout(nextNotice, 5000);

                    if ($(CurrentScrollerDiv + ' div:visible').is(CurrentScrollerDiv + ' div:first-child')) {
                        $(CurrentScrollerDiv + ' div:visible').fadeOut(300);
                        $(CurrentScrollerDiv + ' div:last-child').fadeIn("slow", function() {
                            flag = true;
                        });
                    }
                    else {
                        $(CurrentScrollerDiv + ' div:visible').fadeOut(300).prev('div').fadeIn("slow", function() {
                            flag = true;
                        });
                    }
                    return false;

                });

            });

        };

    })(jQuery);


    $(document).ready(function() {
        //Blog
        $('.itBlog > div:first-child').show();

        //Scroller
        $('.scroller').Scroller();

    });

回答by Lightness Races in Orbit

To build selectors from existing objects, use the second parameter of $:

要从现有对象构建选择器,请使用的第二个参数$

$('div:visible', CurrentScrollerDiv)

Or the findfunction:

或者find功能

CurrentScrollerDiv.find('div:visible');

CurrentScrollerDivis not a string so it cannot be concatenated with a string to generate a string-based selector argument.

CurrentScrollerDiv不是字符串,因此不能与字符串连接以生成基于字符串的选择器参数。



jQuery( selector, [ context ]  )
    jQuery( selector, [context] )    <-- you want this one, and
    jQuery( element )                    `selector` is a string
    jQuery( elementArray )
    jQuery( jQuery object )
    jQuery()
jQuery( html, [ ownerDocument ]  )
    jQuery( html, [ownerDocument] )
    jQuery( html,props )
jQuery( callback  )
    jQuery( callback )
jQuery( selector, [ context ]  )
    jQuery( selector, [context] )    <-- you want this one, and
    jQuery( element )                    `selector` is a string
    jQuery( elementArray )
    jQuery( jQuery object )
    jQuery()
jQuery( html, [ ownerDocument ]  )
    jQuery( html, [ownerDocument] )
    jQuery( html,props )
jQuery( callback  )
    jQuery( callback )

回答by Matt Ball

This is the problematic line:

这是有问题的行:

if ($(CurrentScrollerDiv + ' div:visible').is(CurrentScrollerDiv + '  div:last-child')) {

You using string concatenation on CurrentScrollerDiv, which .toString()s the variable, which is not at all what you want. Don't try to string concatenate jQuery objects or DOM elements. Use jQuery's .find()instead:

您在 上使用字符串连接CurrentScrollerDiv,它.toString()是变量,这根本不是您想要的。不要尝试串接 jQuery 对象或 DOM 元素。使用 jQuery.find()代替:

if (CurrentScrollerDiv.find('div:visible').is(CurrentScrollerDiv.find('div:last-child')) {

There is, however, almost certainly a more efficient way to write that ifstatement.

但是,几乎可以肯定有一种更有效的方法来编写该if语句。

回答by Dmitry

Here is wrong selector:

这是错误的选择器:

var CurrentScrollerDiv = $(this).parent().find('.scroller');

$(CurrentScrollerDiv + ' div:visible') 

fix

使固定

var CurrentScrollerDiv = $(this).parent().find('.scroller');
$('div:visible', CurrentScrollerDiv);