Jquery在同一个类中每三个div进行一次比较
我有一张同一个班的div列表。我想每三个都比较一下,把高度设为最高的。例如:Jquery在同一个类中每三个div进行一次比较,jquery,css,model-view-controller,Jquery,Css,Model View Controller,我有一张同一个班的div列表。我想每三个都比较一下,把高度设为最高的。例如: <ul> <li class="items">box1</li> <li class="items">box2</li> <li class="items">box3</li> <li class="items">box4</li> <li class="items">box5</li>
<ul>
<li class="items">box1</li>
<li class="items">box2</li>
<li class="items">box3</li>
<li class="items">box4</li>
<li class="items">box5</li>
<li class="items">box6</li>
<li class="items">box7</li>
<li class="items">box8</li>
<li class="items">...</li>
</ul>
是否有一种方法可以对列表中的每3个div进行比较,并仅将高度设置为这三个div中的最高值
谢谢大家的回复。对不起,如果我让你感到困惑。我在下面贴了一张图片,让大家更好地理解
在网页上,框1、2、3位于同一行,我想比较哪个框的高度最高,并将其他两个框的高度设置为相同。然后,将方框4、5、6放在新行上,比较这三个选项,并将每个选项设置为最高值。所以第一行和第二行的高度不同,但它们都在同一个类中。在这种情况下,如何将每行设置为不同的高度,因为该行中的最高框。您可以遍历每个第三个div,然后找到另外两个div:
jQuery(document).ready(function($){
$("ul li:nth-child(3n+1)").each(function(){
var first = $(this);
var second = first.next();
var third = second.next();
});
});
因此,使用第n个child,您可以得到每个第3个div和另外2个follower,您可以通过这种方式相互比较。n个child有效。但它不是模块化的。如果希望组大于三个,则需要添加两个步骤。下面是一个替代代码,允许您更改1个数字和分组大小
/*设置我们的计数变量*/
var计数=0;
var高度=0;
var组=0;
var=0;
_组中的_项目的var编号=3;
/*获取列表项的索引长度*/
var indexL=$('li')。长度;
/*每个li项目都是循环的*/
$('li')。每个(函数(索引,元素){
计数++;
/*如果\u组中的\u项的计数%number\u返回anthing,但返回0,则添加高度
如果将3更改为4,它将按4而不是3的组对元素进行计数
*/
if(计数\u组中\u项目的\u百分比){
高度+=$(此).height();
}否则{
/*添加高度、添加到组、设置最高高度并附加到结果*/
组++;
高度+=$(此).height();
计数=0;
如果(高度>最高){
最高=高度
}
$(“#结果”)。追加(“”+高度+”像素:“+组+””);
高度=0;
}
});
/*如果最后一组少于组中的项目数,则仍将
添加最后一个组,即使该组中的项目数量较低*/
if(indexL%组中项目的数量){
组++;
$(“#结果”)。追加(“”+高度+”像素:“+组+””);
高度=0;
}
/*显示最高*/
$(“#结果”)。追加(“”+最高+”像素:最高的“”);
为什么要为此使用脚本,听起来像是CSS问题?你的最终目标是什么?什么决定了最高跳水的高度?
jQuery(document).ready(function($){
$("ul li:nth-child(3n+1)").each(function(){
var first = $(this);
var second = first.next();
var third = second.next();
});
});
/*Sets up our counting variables*/
var count = 0;
var height = 0;
var groups = 0;
var tallest = 0;
var number_of_items_in_group = 3;
/*grabs the index length of list items*/
var indexL = $('li').length;
/*Every li item is looped through*/
$('li').each(function (index, element) {
count++;
/*if the count % number_of_items_in_group returns anthing but a 0 it adds the height
if you change 3 to 4 it will count elements in groups of 4 rather than three
*/
if (count % number_of_items_in_group) {
height += $(this).height();
} else {
/*adds the height, adds to group, sets the tallest, and appends to results*/
groups++;
height += $(this).height();
count = 0;
if (height > tallest) {
tallest = height
}
$('#results').append('<p>' + height + ' Pixels: ' + groups + '</p>');
height = 0;
}
});
/*if the last group is less than number of items in group it will still
add the last group even though it has lower ammount of items in the group*/
if (indexL % number_of_items_in_group) {
groups++;
$('#results').append('<p>' + height + ' Pixels: ' + groups + '</p>');
height = 0;
}
/*shows tallest*/
$('#results').append('<p>' + tallest + ' Pixels: Tallest</p>');