2011-09-19 18 views
5

Praca currenly na aktualności zmieniacza - zobacz moje koncerty przykład tutaj - EXAMPLEUncaught Błąd składni, nierozpoznany wyrażenie: [object Object]

Po naciśnięciu Next/Prev strzałkę Dostaję dziennik błędów Uncaught Syntax error, unrecognized expression: [object Object]

Dlaczego ten problem? Gdzie jest błąd w składni?

kod 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

To jest problem: '$ ('div: visible' CurrentScrollerDiv +)'. Dlaczego myślisz, że możesz połączyć obiekt jQuery z łańcuchem? –

+0

W którym wierszu występuje błąd? –

+0

@ Tomalak Geret'kal - nie mówi, która to linia. – Iladarsda

Odpowiedz

16

Aby zbudować selektorów z istniejących obiektów, należy the second parameter of $:

$('div:visible', CurrentScrollerDiv) 

Albo the find function:

CurrentScrollerDiv.find('div:visible'); 

CurrentScrollerDiv nie jest ciągiem znaków, więc nie może być połączony z ciągiem do generowania ciągu znaków d selektor wyboru.


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 nie jest ciągiem, więc nie można go połączyć za pomocą ciągu znaków w celu wygenerowania argumentu selektora opartego na łańcuchach." - to bardzo przydatna wskazówka. Nie zdawałem sobie z tego sprawy wcześniej. Byłem pewien, że to zwróci ciąg (czysty tekst). – Iladarsda

+0

Nauczyłem się czegoś nowego dzisiaj, zawsze myślałem, że to też się skonwertuje. – Cyprus106

3

Jest to problematyczne linia:

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

używasz ciąg konkatenacji na CurrentScrollerDiv, który .toString() s zmienna, która nie jest w ogóle to, co chcesz. Nie próbuj tworzyć ciągów połączonych obiektów jQuery lub elementów DOM. Używać jQuery .find() zamiast:

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

Jest jednak prawie na pewno bardziej efektywny sposób napisać, że if oświadczeniu.

2

Oto źle selektor:

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

$(CurrentScrollerDiv + ' div:visible') 

fix

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