如何在jquery中增加行末尾的选择器值



我遇到了一个问题,我需要将父div的高度调整为子div中最长文本的长度,但我需要测试一行中的所有div,找到最长文本,并将该宽度用作该行中所有父div高度的值。然后我需要同样的循环函数来增加jQuery选择器,它从选择类名"row-1"到下一行,即row-2开始,然后循环所有这些并设置它们的高度。。。然后转到第三行,第3行,依此类推,直到测试服务器端查询返回的所有行并调整它们的行高。听起来很奇怪,但它看起来是这样的:

http://ccs.btcny.net/redhook/

我使用PHP创建行并添加类名,我有一个函数可以干净快速地输出标记!

但在这种情况下,这有助于PHP,总体逻辑如下:

从第一行中的1个项目开始,然后在循环查询时向每行添加1个项目,直到检索到所有项目。然后在创建新行时缩小所有内容。

我让下面的jquery函数逐行运行。。。但我需要一个循环函数,直到检索到所有项目为止。以下是我能够完成的:

$('.row-1 div.article-list-title').each(function() {
maxWidth = maxWidth > $(this).width() ? maxWidth : $(this).width();
var titwidth = $(this).width();
console.log(titwidth);
});
$('.row-1').each(function() {
$(this).height(maxWidth);
});
$('.row-2 div.article-list-title').each(function() {
maxWidth = maxWidth > $(this).width() ? maxWidth : $(this).width();
var titwidth = $(this).width();
console.log(titwidth);
});
$('.row-2').each(function() {
$(this).height(maxWidth);
});
/* And so on */

非常感谢您的帮助!

提前感谢你对我的帮助!

我想我对你想用这个做什么还不够了解,所以我会保存"我不会这样做"的演讲。

尽管如此,我认为这会给你想要的(如果你想使用的是jQuery)。

heights = new array();
/** Loop through the title classes **/
$('div.article-list-title').each(function(){
var $this = $(this),
currentRow = $this.parent(),
x = 1;
/** Match the parent row class name **/ 
while(! currentRow.hasClass("row-"+ x)) x++;
if (heights.length < x) heights[x] = 0;
if ($this.width() > heights[x]) heights[x] = $this.width();
});
/** Now update all the row-x class heights **/
for (var y = 1; y <= heights.length; y++) {
$(".row-"+y).height(heights[y]);
}

小心使用while(! currentRow.hasClass("row-"+ x)) x++;,如果article-list-title元素没有父row-x类元素,它将永远循环

最新更新