2014-12-04 63 views
0

我希望div id和div類中的數字循環,並且從0到我擁有的許多div。我該如何實現?我試圖把它放在一個for循環,但它並不適用於某些原因..如何在jQuery中創建循環,以便將數字添加到我的div的末尾?

$("#bokautbildning0").hide(); 
$(".boka_btn0").click(function(){ 
     $("#bokautbildning0").slideToggle(); 
     var scrollToId = $(this).attr('href'); 
     $('html, body').animate({ 
      scrollTop: $(scrollToId).offset().top - 270 
     }, 500); 
     return false; 
    }); 


$("#bokautbildning1").hide(); 
$(".boka_btn1").click(function(){ 
     $("#bokautbildning1").slideToggle(); 
     var scrollToId = $(this).attr('href'); 
     $('html, body').animate({ 
      scrollTop: $(scrollToId).offset().top - 270 
     }, 500); 
     return false; 
    }); 


$("#bokautbildning2").hide(); 
$(".boka_btn2").click(function(){ 
     $("#bokautbildning2").slideToggle(); 
     var scrollToId = $(this).attr('href'); 
     $('html, body').animate({ 
      scrollTop: $(scrollToId).offset().top - 270 
     }, 500); 
     return false; 
    }); 

下面是與迴路我試過代碼:

for (var i = 0; i < 5; i++) { 
    $("#bokautbildning" + i).hide(); 
    $(".boka_btn" + i).click(function(){ 
     $("#bokautbildning" + i).slideToggle(); 
     var scrollToId = $(this).attr('href'); 
     $('html, body').animate({ 
      scrollTop: $(scrollToId).offset().top - 270 
     }, 500); 
     return false; 
    }); 
    } 
+0

你的問題不是很清楚。你想要完成什麼,循環在哪裏? – Aashray 2014-12-04 11:28:16

+0

爲什麼不給他們所有的班級,而不是給班級編號?如果它們對於每個DIV都是唯一的,那麼類不是很有用。 – Barmar 2014-12-04 11:28:56

+1

你在說什麼1號? '.boka_btn'或者'#bokautbildning1'後面的那個? – Barmar 2014-12-04 11:29:45

回答

0

,如果你利用了數據屬性的jQuery的因爲這將是非常簡單的。即:

HTML示例:

<div data-number="1"></div> 
<div data-number="2"></div> 
<div data-number="3"></div> 
<div data-number="4"></div> 
<div data-number="5"></div> 

的jQuery:

$("div[data-number]").each(function() { 
    var number = $(this).data("number"); 
    $(this).html(number); 
    alert(number); 
}) 

要看到它在行動:

http://jsfiddle.net/uubxj55r/

+1

這是一個非常漫長的做法。你可以使用每個循環的索引。不需要數據屬性。 – 2014-12-04 13:27:30

+0

因此,爲您的頁面上的每個div創建1個類的反模式會更長時間,然後使用data-attribute存儲數字並查詢div是否具有數據編號屬性...? – 2014-12-04 13:31:10

+1

謝謝!數據屬性和每個函數都是更好的解決方案! – 2014-12-04 13:33:10

1

這個答案寫對於舊版本的問題,不再正確。

最好的解決方案是將一個類添加到所有這些元素,並使用它來代替ID來運行jQuery。讓jQuery做這個工作來遍歷所有這些元素。

例如,給所有元素#bokautbildningn.bokautbildning,改變你的JavaScript代碼如下:

$('.bokautbildning').hide(); 

$('.boka_btn1').click(function(){ 
    $('.bokautbildning').slideToggle(); 

    var scrollToId = $(this).attr('href'); 
    $('html, body').animate({ 
     scrollTop: $(scrollToId).offset().top - 270 
    }, 500); 

    return false; 
}); 
+0

更新我的問題,非常感謝任何幫助 – 2014-12-04 13:08:40

0

您需要使用Each和公正使確保每個部分中的所有項目都具有相同的類別。沒有必要使用您當前使用的編號系統。

$('.MyDivs').each(function(index){ //Loop through all the elements with class of 'MyDivs' 
    var thisElement = $(this); //get the current element we're on in the loop 
    thisElement.find('.bokautbildning').slideToggle(); //Find the element with class 'bokautbildning' within the current element in the loop and slideToggle it 
    thisElement.find('.boka_btn').click(function() { //Find the element with classs 'boka_btn' within the current element in the loop and add a click listener 
     //Code for click event happens here 
    }); 
} 

下面是一個例子:http://jsfiddle.net/o85tk0s3/

+0

我需要數字系統的另一個原因在PHP代碼..但感謝您的提示! :) – 2014-12-04 13:34:15

相關問題