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>

在此处输入图像描述

4

2 回答 2

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 的回调函数由大括号终止,但它只是each()命令的参数。

于 2013-02-23T10:59:36.173 回答
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

于 2013-02-23T11:01:06.620 回答