2013-04-29 42 views
0
$(function() { 
    $max = 4; 
    $i = 1; 
    for ($i = 1; $i < 3; $i++) { 
     $('#itemListLeading table tr :nth-child(' + $i + ')').each(function() { 
      $length = $(this).children().text().length; 
      if ($length > $max) { 
       $max = $length; 
      } 
      $('#itemListLeading table tr :nth-child(' + $i ' +)').promise().done(function() { 
       $(this).css("min-width", $max * 8 + "px"); 
      }); 
     }); 
    }; 
}); 

不確定爲什麼這裏不起作用。我試圖通過'1'和'2',也許更多後來到第n個孩子()。它適用於單個孩子(1),即。沒有for循環。還是有更好的方法來傳遞x個變量來運行函數?For loop將變量傳遞到函數上

Here is a fiddle

+0

http://jsfiddle.net/xNquz/7/鏈接在這裏,因爲它不會讓我張貼。 – Supernal 2013-04-29 13:41:21

+3

JavaScript不是PHP;你的變量名不會以'$'開頭。 – 2013-04-29 13:43:36

+0

你還沒有真正解釋問題是什麼。 – 2013-04-29 13:44:13

回答

0

你有一個語法錯誤在你的腳本:

$(function() { 
    $max = 4; 
    $i = 1; 
    for ($i = 1; $i < 3; $i++) { 
     $('#itemListLeading table tr :nth-child(' + $i + ')').each(function() { 
      $length = $(this).children().text().length; 
      if ($length > $max) { 
       $max = $length; 
      } 
      // Old line 
      //$('#itemListLeading table tr :nth-child(' + $i ' +)').promise().done(function() { 

      $('#itemListLeading table tr :nth-child(' + $i + ')').promise().done(function() { 
       $(this).css("min-width", $max * 8 + "px"); 
      }); 
     }); 
    }; 
}); 
+0

啊耶謝謝。似乎現在至少執行,但崩潰我的瀏覽器,並要求我停止腳本。它只是陷入循環? – Supernal 2013-04-29 13:58:29