2014-07-15 14 views
0

我有一組我通過每3秒想週期有關的圖像,但只顯示一次一個:jQuery的下一個()無法正常運行

# some django template code: 
<div id="featured-prints" class="featured-promo"> 
    {% for p in promo_prints %} 
    <a href="{% url 'print_order_with' p.id %}"> 
     <img class="featured-print" src="{{ MEDIA_URL }}{{ p.id }}_{{ THUMBNAIL_SIZE }}.png" alt="{{ p.body }}" /> 
    </a> 
    {% endfor %} 
</div> 

# the JS to cycle through: 

$(function(){ 
    $('.featured-promo a:gt(0)').hide(); 
    setInterval(function(){ 
     $('.featured-promo :first-child') 
     .hide() 
     .next('a') 
     .fadeIn(2000) 
     .end() 
     .appendTo('.featured-promo'); 
}, 1000); 

我最近添加的<a>各地img,現在Javascript不起作用;它不會fadeIn()下一個鏈接的圖像。我已經嘗試了幾種變化,包括將「a」傳遞給next()和「img」,但似乎沒有任何效果。我也嘗試鏈接parent()hide()函數,仍然沒有骰子。

任何提示?

+1

'淡入(2000)' - '2000毫秒'和'1000ms'的間隔。那絕對不行! –

+0

出於好奇,你爲什麼反覆追加'.featured-promo'?爲什麼你在淡入之前再次運行間隔?你知道你在做什麼,或者你只是在隨機查詢功能..? –

+0

@TilwinJoy他似乎在做的是淡化第二個元素,然後將第一個元素移動到最後,所以第二個元素成爲第一個元素。這將繼續旋轉傳送帶。 – Barmar

回答

2

嘗試改變

$('.featured-promo :first-child') 

到:

$('.featured-promo > :first-child') 

沒有>它下降到每一個層次。所以它找到.featured-promo(第一個a)的第一個孩子,並且每個a(每img)的第一個孩子。它隱藏了所有這些,然後在接下來的a中褪色。 img標籤保持隱藏狀態,因爲沒有任何東西會使他們淡入。

>在選擇器中意味着只將下一部分與直接子代進行匹配,而不是所有後代。

+0

這不排序我害怕的問題;沒有任何圖像仍然顯示。我曾嘗試在下一個()調用中放入'img'和'a',但無濟於事。 –

+0

你可以做一個jsfiddle嗎? – Barmar

+0

@ThePiedPipes這樣做[排序問題](http://jsfiddle.net/4TkS4/)據我所知.. ..?我無法理清這個問題仍然看到這個帖子,我是誰投了票...問題可能是你的代碼中的其他東西..? –

0
$(function(){ 
    $('.featured-promo a:gt(0)').hide(); 
    setInterval(function(){ 
    $('.featured-promo :first-child') 
    .hide() 
    .find('a') 
    .fadeIn(2000) 
    .end() 
    .appendTo('.featured-promo'); 
}, 1000); 

嘗試找到的,而不是未來

+0

這不行,我恐怕。 –

0

使用結束()隱藏後():

$('.featured-promo :first-child') 
     .hide() 
     .end() 
     .next('a') 
     .fadeIn(2000) 
     .end() 
     .appendTo('.featured-promo'); 
+0

恐怕不行。 –

0

試試這個:

$(function(){ 
    $('.featured-promo a:gt(0)').hide(); 
    setInterval(function(){ 
     $('.featured-promo').children('a .featured-print').eq(1).parent() 
     .hide() 
     .next('a') 
     .fadeIn(2000) 
     .end() 
     .appendTo('.featured-prints'); 
}, 1000); 
+1

'.featured-print'不是'.featured-promo'的孩子,它是一個大孩子。 – Barmar

+0

@Barmar對不起,修好了! –

1

的錯誤是在那裏

$(function(){ 
    $('.featured-promo a:gt(0)').hide(); 
    setInterval(function(){ 
    $('.featured-promo :first-child')// here 
    .hide() 
    .next('a') 
    .fadeIn(2000) 
    .end() 
    .appendTo('.featured-promo');// no need to append as the for loop is already appending the anchors to the featured-promo. 
    }, 1000); 
)}; 

您所呼叫的.next('a').featured-promo第一胎是不是.featured-promo兄弟,但它的孩子。 elementA.next()用於獲取兄弟(的elementA即B元素後的元素)

要得到其他a S'你應該這樣寫

$(function(){ 
    var index = 0; 
    var total_a = $('.featured-promo a').length; 
    setInterval(function(){ 
    $('.featured-promo a:gt(' + index + ')') 
    .hide() 
    .next('a') 
    .fadeIn(2000); 
    index = (index < total_a) ? index + 1 : 0;// will increment if number of a is greater than index else 0 and continue like a slider.. 
    }, 2000);// better use 2000 ms as you're using 2000ms in the `fadeIn()` 
}); 
+1

你沒有關閉就緒功能。 –

+0

啊,我的不好。感謝您的通知。 – Xlander

+0

我不明白你的觀點。他不想要一個'.featured-promo'的兄弟,他想要第二個孩子。 – Barmar