找到最长的元素(jQuery)
我使用此脚本来均衡元素的高度:
(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);
我想为其添加一项额外功能 - 将“最长”类添加到均衡高度时找到的最长元素,我在上面的脚本中需要更改什么?
非常感谢。
I use this script to equalize heights of elements:
(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);
I'd like to add just one extra functionality to it - addclass 'longest' to the longest element found when equalizing heights, what do I change in the above script?
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你说的史蒂夫克拉里奇的上述解决方案不起作用 - 对我来说效果很好; http://jsfiddle.net/ZqFp5/ (仅在 Chrome 中测试)
尽管使用
选择器效率有些低下在大型 DOM 中,如果可能,请考虑向 div 添加一个类以使用更具体的选择器。
Steve Claridge's above solution you say doesn't work - works fine for me; http://jsfiddle.net/ZqFp5/ (tested in chrome only)
Though using the
selector is somewhat inefficient in a large DOM, consider adding a class to the div's to use a more specific selector if possible.
考虑这个伪代码比任何东西都多,因为它还没有经过测试(甚至没有运行)。更改了 //新代码注释中的代码
Consider this more pseudo-code than anything as it hasn't been tested (or even run). Changed code inside //new code comment
我记得偶然发现了这个网站。这有帮助吗?
http://www.queness.com/ post/126/useful-and-handy-jquery-tips-and-tricks
阅读数字 10。等高的列。
I remember stumbling across this site. Does this help?
http://www.queness.com/post/126/useful-and-handy-jquery-tips-and-tricks
Read number 10. Columns of equal height.