未捕获的语法错误,无法识别的表达式:[目标对象]

问题描述:

在新闻滚动currenly工作 - 看我住在这里的例子 - EXAMPLE未捕获的语法错误,无法识别的表达式:[目标对象]

当我按下一个/上箭头我得到一个错误日志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 < $(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(); 

    }); 
+1

这就是问题所在:'$(CurrentScrollerDiv + 'DIV:可见')'。你为什么认为你可以连接一个字符串的jQuery对象? –

+0

错误发生在哪条线上? –

+0

@Tomalak Geret'kal - 它没有说明哪一行。 – Iladarsda

新建从现有的对象选择,使用the second parameter of $

$('div:visible', CurrentScrollerDiv) 

或者the find function

CurrentScrollerDiv.find('div:visible'); 

CurrentScrollerDiv不是一个字符串,因此它不能连接到一个字符串以生成一个字符串基d选择器参数。


​​
+1

'CurrentScrollerDiv不是一个字符串,所以它不能与一个字符串连接以生成一个基于字符串的选择器参数。“ - 这个非常有用的提示。我以前没有意识到这一点。我确信这会返回字符串(纯文本)。 – Iladarsda

+0

今天学到了新东西,我一直认为它也会转换成字符串。 – Cyprus106

这是问题的行:

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

您使用上CurrentScrollerDiv字符串连接,这.toString() S中的变量,这是不是在所有你想要的。不要试图串联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);