【发布时间】:2014-11-21 10:02:56
【问题描述】:
我的目标是根据元素所在位置的数据集对页面上的元素进行分组。内容已经被渲染到页面上,我想要一个 Location 标头,用于在其中举行的事件上方的每个唯一位置。页面的结构是类名 > 位置 > 上课时间
我正在做几个 .each() 循环来尝试找到我需要的东西,但是我认为应该是第一个每个循环的单独运行的值都被组合在一起。问题是,每个循环仍在运行多次。
$('.class-Info').each(function ()
{
var className = $("h2").text();
var eventLocations = [];
$('.scheduledClass').each(function (index)
{
eventLocations[index] = $(this).data("location");
});
var key = "";
var uniqueLocations = [];
$(eventLocations).each(function (index, location)
{
if (key !== eventLocations[index])
{
key = eventLocations[index];
uniqueLocations.push(eventLocations[index]);
}
});
console.log ("For " + className + " the locations are " + uniqueLocations);
});
这是我的代码。我希望我的问题是有道理的。看看控制台看看我得到的结果。
【问题讨论】:
-
刚刚做了一个小编辑,我意识到在
.scheduledClass元素的每个循环期间它没有得到正确的className。必须添加相同的$(this).find('h2')