2

我使用这个脚本来平衡元素的高度:

(function ($) {
    $.fn.autoheight = function () {
        var height = 0,
            reset = $.browser.msie ? "1%" : "auto";
        return this.css("height", reset).each(function () {
            height = Math.max(height, this.offsetHeight);
        }).css("height", height).each(function () {
            var h = this.offsetHeight;
            if (h > height) {
                $(this).css("height", height - (h - height));
            };
        });
    };
})(jQuery);

我想为其添加一个额外的功能 - 将类“最长”添加到均衡高度时发现的最长元素,我在上面的脚本中要改变什么?

非常感谢。

4

3 回答 3

2

您说的史蒂夫·克拉里奇(Steve Claridge)的上述解决方案不起作用-对我来说很好;http://jsfiddle.net/ZqFp5/(仅在 chrome 中测试)

虽然使用

 $("*")

选择器在大型 DOM 中效率较低,如果可能,请考虑向 div 添加一个类以使用更具体的选择器。

 $(".foo") 
于 2010-03-03T17:41:42.273 回答
1

考虑这个比任何东西都更伪代码,因为它没有经过测试(甚至运行)。//新代码注释内的更改代码

(function ($) {
    $.fn.autoheight = function () {
        var height = 0,
            highest = 0, //new code
            reset = $.browser.msie ? "1%" : "auto";
        return this.css("height", reset).each(function () {
            height = Math.max(height, this.offsetHeight);
            //new code
            if (height > highest) {
              highest = height;
              $("*").removeClass("longest");
              $(this).addClass("longest"); 
            };
            //new code
        }).css("height", height).each(function () {
            var h = this.offsetHeight;
            if (h > height) {
                $(this).css("height", height - (h - height));
            };
        });
    };
})(jQuery);
于 2010-03-03T14:46:01.857 回答
0

我记得偶然发现了这个网站。这有帮助吗? http://www.queness.com/post/126/useful-and-handy-jquery-tips-and-tricks

阅读数字 10。等高的列。

于 2010-03-03T12:22:02.943 回答