2013-02-23 61 views
0

我無法編寫每個函數類 每當我編寫以下內容時,它給我錯誤的原因是什麼?無法在函數類中寫入每個函數

<script> 
     var addLi = function($li) { 
      var $uls = $('ul'); 
      $.each($uls, function(index, ul) { 
       // If this ul has less than 5 li's 
       // then add the li here 
       if ($(this).find('li').length < 5) { 
        $(this).append($li); 
        // exit the for loop since we have placed this li 
        return; 
       } 
      } 
      }; 
    </script> 

enter image description here

+1

錯過);的截圖已經具備了,爲什麼你:'預期 ')''。解析器期望在那裏有一個''''''。你不明白的是什麼? – 2013-02-23 11:02:08

回答

2

你根本沒有關閉.each()命令。

$.each($uls, function(index, ul) { 
    // If this ul has less than 5 li's 
    // then add the li here 
    if ($(this).find('li').length < 5) { 
    $(this).append($li); 
    // exit the for loop since we have placed this li 
    return; 
    } 
}); // <---------- here is the closing brackets for the each() 

爲每個回調函數是由大括號終止,但它僅僅是爲了each()命令的參數。

0

試試這個

<script> 
    var addLi = function($li) { 
     var $uls = $('ul'); 
     $.each($uls, function(index, ul) { 
      // If this ul has less than 5 li's 
      // then add the li here 
      if ($(this).find('li').length < 5) { 
       $(this).append($li); 
       // exit the for loop since we have placed this li 
       return; 
      } 
     }); // close brace missing here before 
    }; 
</script> 

你爲$.each