我试图建立一个小的jQuery插件,但是我收到一个错误,说group.height()不是一个函数?

(function( $ ) {

    $.fn.equalHeight = function(group) {
        group = $.extend(group);

        var tallest = 0;
        group.each(function () {
            var thisHeight = $(this).height();
            if (thisHeight > tallest) {
                tallest = thisHeight;
            }
        });

        group.height(tallest);

        // allow jQuery chaining
        return this;
    };

}( jQuery ));


用法示例如下所示:

<script>
    // Usage example:
    $( ".boxes section.box" ).equalHeight();
</script>

最佳答案

尝试

(function ($) {

    $.fn.equalHeight = function () {
        var tallest = 0;
        this.each(function () {
            var thisHeight = $(this).height();
            if (thisHeight > tallest) {
                tallest = thisHeight;
            }
        });

        // allow jQuery chaining
        return this.height(tallest);
    };
}(jQuery));


演示:Fiddle

10-06 00:15