2011-05-12 30 views
2

我使用了一個新聞股票在一個頁面中的新聞欄目,我想用prettyphoto插件與這些消息,但任何jQuery函數不工作時< LI>項目改變他們的立場。jQuery函數不與股票新聞工作

例如,當第一項將成爲最後一個沒有jQuery的功能不起作用

這裏是代碼

$(document).ready(function(){ 
    var first = 0; 
    var speed = 1000; 
    var pause = 3500; 

    function removeFirst(){ 
     first = $('ul#listticker li:first').html(); 
     $('ul#listticker li:first') 
     .animate({opacity: 0}, speed) 
     .fadeOut('slow', function() {$(this).remove();}); 
     addLast(first); 
    } 

    function addLast(first){ 
     last = '<li style="display:none">'+first+'</li>'; 
     $('ul#listticker').append(last) 
     $('ul#listticker li:last') 
     .animate({opacity: 1}, speed) 
     .fadeIn('slow') 
    } 

    interval = setInterval(removeFirst, pause); 

     //Codes above are for the news ticker 

    $(".news").click(function(e){ 
     e.preventDefault(); //I assign news class to links if there is no image for the news on db but it doesn't even work 
    }); 

    $("#newsdiv a[rel^='gallery']").prettyPhoto({ 
     theme:'light_rounded' 
    }); 
}); 

和PHP函數的HTML結果

<ul id="listticker"> 
    <li> 
     <img src="http://example.com/m.../k_haber.png"><a href="#" class="news">12.05.2011</a><span class="news-text">Some Title</span> 
    </li> 
    <li> 
     <img src="http://example.com/../some.png"><a href="http://example.com/../news/p_some.jpg" class="news-title" rel="gallery[dd0]"> 12.05.2011</a><span class="news-text">Some Other Title</span> 
    </li> 
</ul> 

任何想法可能會導致這種情況或如何解決它?

編輯
我想是因爲jQuery的html選擇的問題。

回答

0

我意識到jQuery函數shuld其將被用於這樣最後,我發現溶液中的函數內聲明。

這裏是代碼

$(document).ready(function(){ 
    var first = 0; 
    var speed = 1000; 
    var pause = 3500; 

    function removeFirst(){ 
     first = $('ul#listticker li:first').html(); 
     $('ul#listticker li:first') 
     .animate({opacity: 0}, speed) 
     .fadeOut('slow', function() {$(this).remove();}); 
     addLast(first); 
    } 

    function addLast(first){ 
     last = '<li style="display:none">'+first+'</li>'; 
     $('ul#listticker').append(last) 
     $('ul#listticker li:last') 
     .animate({opacity: 1}, speed) 
     .fadeIn('slow',function(){ 
      $("a[rel^='gallery']").click(function() { 
       $.prettyPhoto.open($(this).attr("href"),"",""); 
       return false; 
      }); 
     }); 
     $(".news").click(function(e){ 
      e.preventDefault(); 
     }); 
    } 

    interval = setInterval(removeFirst, pause); 


    $("#newsdiv a[rel^='gallery']").prettyPhoto({ 
     theme:'light_rounded' 
    }); 
}); 
0

讓您的變量和函數的聲明DOM已準備就緒事件之外。 http://jsfiddle.net/jfahv/

+0

感謝,但它也不能工作,甚至在的jsfiddle,奇怪的是,在當第二變成第一的第一個週期,第一個作品,但不是第二個和它的第2個週期沒有works..http後://jsfiddle.net/jfahv/6/(和我的英語對不起) – 2011-05-12 18:52:25

+0

可能是'的console.log()'離開了那裏無意的。它爲我的作品都ie7-9和FF4 – Zlatev 2011-05-12 18:56:07

0

jQuery的「:第一個」選擇返回第一個匹配的元素,而不是第一個子元素。嘗試使用:第一個孩子。

+0

感謝,但也不能工作 – 2011-05-12 19:16:43