2012-03-06 29 views
1

http://gidzior.net/svg/pom01.html面板的右邊是「#右面板禮」,紅色按鈕是「div.save」指數變化每次我點擊一個按鈕

從「#右面板按鈕li'應該打開一個窗口並按下'div.save'應該關閉它,然後'#右側面板li'應該再次打開它

當我點擊數字從1到9的按鈕時,腳本放置0 befor數字1-9和索引是01,02,03 ...,當我再次點擊索引是001,002,003,等等,我怎麼設置索引保持不變時間 ?? 01,02,03,04,05,06,07,08,09

//OPEN A WINDOW 
    $('#right-panel li').each(function(e){ 
     var i = $(this).index()+1; 
     $(this).click(function(){ 
      if(i<=9){ 
       i = "0"+i; 
      } 
      $('#pom01par'+i+'WraperAbsolute').fadeIn("fast"); 
      $('#pom02par'+i+'WraperAbsolute').fadeIn("fast"); 
      $('#pom03par'+i+'WraperAbsolute').fadeIn("fast"); 
      $('#pom04par'+i+'WraperAbsolute').fadeIn("fast"); 
      $('#pom05par'+i+'WraperAbsolute').fadeIn("fast"); 
      $(this).css('background','#adff84'); 
      $('.par'+i+'table').css('background','#adff84'); 
      return false; 
     }); 
    }); 


    // CLOSE A WINDOW 
    $('div.save').click(function(){ 
     $(this).parent().fadeOut("fast"); 
    }); 
+0

我不是一個數字,它是在上下文的字符串。 – 2012-03-06 08:41:05

+0

@EvilP:它有所不同。它以一個數字開始,然後變成一個字符串。 – 2012-03-06 09:16:06

回答

1

,在跳出我的一個問題是:

if(i<=9){ 
    i = "0"+i; 
} 

要修改我的價值;

而是使用另一個變量的點擊功能:

$('#right-panel li').each(function(e){ 
     var i = $(this).index()+1; 
     $(this).click(function(){ 
      var this_i = i; 
      if(this_i<=9){ 
       this_i= "0"+i; 
      } 
      $('#pom01par'+this_i+'WraperAbsolute').fadeIn("fast"); 
      $('#pom02par'+this_i+'WraperAbsolute').fadeIn("fast"); 
      $('#pom03par'+this_i+'WraperAbsolute').fadeIn("fast"); 
      $('#pom04par'+this_i+'WraperAbsolute').fadeIn("fast"); 
      $('#pom05par'+this_i+'WraperAbsolute').fadeIn("fast"); 
      $(this).css('background','#adff84'); 
      $('.par'+this_i+'table').css('background','#adff84'); 
      return false; 
     }); 
    }); 
+0

作品完美,THX很多 – gidzior 2012-03-06 08:51:00

+0

我的意思是完美的:] – gidzior 2012-03-06 12:05:21

0

我想你應該在click(功能的範圍界定i,或者更好yet-完全不定義它:

$(this).click(function(){ 
var iAsString = $(this).index()+1 + "";    
if($(this).index()+1<=9){ 
       iAsString = "0"+iAsString; 
      } 
+0

如何寫這個條件爲真,只有01,02,03 ... 09 ?? – gidzior 2012-03-06 08:42:37

-1

我不明白你爲什麼要做這樣的事情,但我帶了一個很短的解決方案。 嘗試修改情況這樣

if(i<=9 && i.length==1){ 
    i = "0"+i; 
} 

這樣你會檢查i兩個值爲數字和字符串。

+0

呃? 'length'不是一個函數,你錯過了一個'='...... – elclanrs 2012-03-06 08:43:01

+0

'length'是一個字符串的屬性,而不是函數。並且一個條件是用兩個'='寫的,比如'if(i.length == 1)i =「0」+ i;' – 2012-03-06 08:43:18

+0

是的,我犯了一個錯誤,寫得很快......回答編輯 – themarcuz 2012-03-06 08:44:32

0

我不確定錯誤。

但我嘗試做我自己的索引。

變化

var i = $(this).index()+1; 

通過

var i = 1; 
在循環結束

放我++;

$('#right-panel li').each(function(e){ 
...... 
i++; 
}