无法在函数类中编写每个函数

时间:2013-02-23 10:57:06

标签: javascript jquery html

我无法编写每个函数类 无论何时我写下以下内容,它都会给我错误原因?

<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

2 个答案:

答案 0 :(得分: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()命令的参数。

答案 1 :(得分: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