2011-09-19 15 views
5

Trabajando currenly de teletipo - ver mi ejemplo vivo aquí - EXAMPLEError de sintaxis no detectada, expresión no reconocida: [object Object]

Cuando pulso siguiente/ant flecha Recibo un registro de errores Uncaught Syntax error, unrecognized expression: [object Object]

¿Por qué es el problema? ¿Dónde está el error en la sintaxis?

Código 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

Ese es el problema: '$ (+ CurrentScrollerDiv 'div: Visible')'. ¿Por qué crees que puedes concatenar un objeto jQuery con una cadena? –

+0

¿En qué línea se produce el error? –

+0

@Tomalak Geret'kal - no dice qué línea es. – Iladarsda

Respuesta

16

Para construir selectores de objetos existentes, utilice the second parameter of $:

$('div:visible', CurrentScrollerDiv) 

O the find function:

CurrentScrollerDiv.find('div:visible'); 

CurrentScrollerDiv no es una cadena para que no pueda ser concatenado con una cadena para generar una cadena de base d argumento del selector.


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) 
+1

'CurrentScrollerDiv no es una cadena, por lo que no se puede concatenar con una cadena para generar un argumento de selector basado en cadenas. - ese consejo muy útil. No me había dado cuenta de eso antes. Estaba seguro de que esto devolverá cadena (texto puro). – Iladarsda

+0

Aprendí algo nuevo hoy, siempre pensé que se convertiría en una cadena también. – Cyprus106

3

Ésta es la línea problemática:

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

Usted mediante la concatenación de cadenas en CurrentScrollerDiv, que .toString() s la variable, lo cual no es en absoluto lo que quiere. No intente encadenar concatenar objetos jQuery o elementos DOM. Usar jQuery de .find() lugar:

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

Hay, sin embargo, es casi seguro que una manera más eficiente escribir que if comunicado.

2

Aquí es selectora equivocada:

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

$(CurrentScrollerDiv + ' div:visible') 

solución

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