2013-02-23 78 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