未捕获的语法错误,无法识别的表达式:

当前在新闻滚动条上工作 – 请参阅我的实例 – 示例

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

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

jQuery代码:

  (function($) { /*! Scroller ---------------------------------------------*/ $.fn.Scroller = function() { //Set height $('.scroller').each(function() { var height = 0; $(this).children('div').each(function() { if (height  div:first-child').show(); //Scroller $('.scroller').Scroller(); }); 

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

 $('div:visible', CurrentScrollerDiv) 

或者findfunction :

 CurrentScrollerDiv.find('div:visible'); 

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 ) 

这是有问题的一行:

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

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

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

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

这是错误的选择器:

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

固定

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