我正在试图弄清楚如何p通过p在不设置最大高度的情况下动态查找该div中最长元素的高度来设置div 中所有兄弟元素的高度,从此处获取代码
我想动态地将p动态高度设置为最长,因为我不知道最长的高度p
这是代码
$(document).ready(function() {
setHeight('.col');
});
//global variable, this will store the highest height value
var maxHeight = 100;
function setHeight(col) {
//Get all the element with class = col
col = $(col);
//Loop all the col
col.each(function() {
//Store the highest value
if($(this).height() > maxHeight) {
maxHeight = $(this).height();;
}
});
//Set the height
col.height(maxHeight);
}
Run Code Online (Sandbox Code Playgroud)
如果有人知道如何做到这一点会很棒
我有一个原始的JavaScript解决方案,但它必须是jquery
function parseRightTabs() {
var height = 20;
var ht = 0;
for(var i=1; i<5; i++) {
ht = Od('rTest'+i).offsetHeight;
if(ht>height) height = ht;
if(i>1)Od('rTest'+i).style.display='none';
}
if(height < 50) height = 112;
Od('rTests').style.height = height + 'px';
Od('rtShow2').style.display = Od('rtShow3').style.display=Od('rtShow4').style.display = 'none';
}
Run Code Online (Sandbox Code Playgroud)
希望有人能提供帮助
这是链接,如果您单击右侧部分中的推荐并单击1,2或3
这是js小提琴
试试这个 -
var $paragraphs = $('div p');
var heights = $paragraphs.map(function() {
return $(this).height();
});
var maxHeight = Math.max.apply(this, heights);
$paragraphs.height(maxHeight);
Run Code Online (Sandbox Code Playgroud)