2011-09-19 12 views
5

Lavorare currenly sulle notizie scroller - vedi il mio esempio vivo qui - EXAMPLEUncaught Errore di sintassi, l'espressione non riconosciuto: [object Object]

Quando premo successivo precedente freccia/sto ricevendo un log degli errori Uncaught Syntax error, unrecognized expression: [object Object]

Perché il problema? Dov'è l'errore nella sintassi?

codice 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

Questo è il problema: '$ (+ CurrentScrollerDiv 'div: visibile')'. Perché pensi di poter concatenare un oggetto jQuery con una stringa? –

+0

In quale riga si verifica l'errore? –

+0

@Tomalak Geret'kal - non dice quale linea è. – Iladarsda

risposta

16

Costruire selettori da oggetti esistenti, utilizzare the second parameter of $:

$('div:visible', CurrentScrollerDiv) 

O the find function:

CurrentScrollerDiv.find('div:visible'); 

CurrentScrollerDiv non è una stringa in modo che non possa essere concatenato con una stringa per generare una base di stringhe d argomento selettore.


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 non è una stringa, quindi non può essere concatenata con una stringa per generare un argomento selettore basato su stringhe. - Questo suggerimento molto utile. Non me ne sono reso conto prima. Ero sicuro che questo restituirà una stringa (puro testo). – Iladarsda

+0

Ho imparato qualcosa di nuovo oggi, ho sempre pensato che sarebbe anche convertito in stringa. – Cyprus106

3

Questa è la linea problematica:

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

Si utilizza concatenazione di stringhe in CurrentScrollerDiv, che .toString() s la variabile, che non è affatto ciò che si desidera. Non provare a stringare concatenare oggetti jQuery o elementi DOM. Utilizzare jQuery di .find() invece:

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

V'è, tuttavia, quasi certamente un modo più efficiente per scrivere che if dichiarazione.

2

Ecco selettore sbagliato:

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

$(CurrentScrollerDiv + ' div:visible') 

correzione

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