将文本发送到数组中的特定 li
Send text to specific li in an array
我在 ul 中有许多 li 项。我需要将所有 li 项目添加到一个数组,然后遍历该数组并对每个 li 中的值求和。
该值表示该项目将花费的小时数。所以第一项可能是 2 小时,第二项可能是 5 小时。
每 7.5 小时,我需要在每一里的天字段中添加 1 天。因此项目 1、2 和 3 将显示第 1 天。项目 4、5、6 和 7 将显示第 2 天等
这是我目前的情况:
列表数组:
var list = document.getElementById("dropArea").getElementsByTagName("li");
小时数:
var hrsArray = $("#sortable2 li").find("#hrsSpan").map(function () { return $(this).text() }).get();
var lengthArr = hrsArray.length;
for (var i = 0; i < lengthArr; i++) {
hrsArray[i] = hrsArray[i].replace("Hours - (", "");
hrsArray[i] = hrsArray[i].replace(")", "");
}
这是我计算的总小时数。我可以将“1”发送到每个里的日期跨度,但我不知道如何单独查看这些里:
//Add all the hrs together to get the total
for (var i in hrsArray) {
total += hrsArray[i];
//alert(list[i].toString());
//Object found at this point, need to figure out how to send text to day span in it.
if (total / 7.5 <= 1) {
$('#sortable2 li').find('#day').html('1');
}
}
当您执行 $('#sortable2 li').find('#day').html('1');
时,您会丢失找到的 jquery 对象。您需要再次将其包装在 $() 中。这是无需使用查找的更简单方法。
$("#sortable2 #day").html(1)
这是一个工作示例http://jsfiddle.net/9nutmuvm/
$('#sortable2 li').find('#day')
这将创建一个包含所有匹配对象的集合,使用 .get(index) 检索特定对象。
http://api.jquery.com/get/
$('#sortable2 li').find('#day').get(i).html('1');
为了避免在每次迭代时重建集合,我会将其存储在循环外的变量中。
//Add all the hrs together to get the total
var $dayFields = $('#sortable2 li').find('#day');
for (var i in hrsArray) {
total += hrsArray[i];
//alert(list[i].toString());
//Object found at this point, need to figure out how to send text to day span in it.
if (total / 7.5 <= 1) {
$($dayFields.get(i)).html('1');
}
}
编辑:
解决这个问题的更好方法是遍历每个 li 而不是小时数组:
$("#sortable2 li").each(function() {
$(this).find("hrsSpan"); // This selects the hours field
$(this).find("day"); // This selects the day field in the same li
});
我在 ul 中有许多 li 项。我需要将所有 li 项目添加到一个数组,然后遍历该数组并对每个 li 中的值求和。
该值表示该项目将花费的小时数。所以第一项可能是 2 小时,第二项可能是 5 小时。
每 7.5 小时,我需要在每一里的天字段中添加 1 天。因此项目 1、2 和 3 将显示第 1 天。项目 4、5、6 和 7 将显示第 2 天等
这是我目前的情况:
列表数组:
var list = document.getElementById("dropArea").getElementsByTagName("li");
小时数:
var hrsArray = $("#sortable2 li").find("#hrsSpan").map(function () { return $(this).text() }).get();
var lengthArr = hrsArray.length;
for (var i = 0; i < lengthArr; i++) {
hrsArray[i] = hrsArray[i].replace("Hours - (", "");
hrsArray[i] = hrsArray[i].replace(")", "");
}
这是我计算的总小时数。我可以将“1”发送到每个里的日期跨度,但我不知道如何单独查看这些里:
//Add all the hrs together to get the total
for (var i in hrsArray) {
total += hrsArray[i];
//alert(list[i].toString());
//Object found at this point, need to figure out how to send text to day span in it.
if (total / 7.5 <= 1) {
$('#sortable2 li').find('#day').html('1');
}
}
当您执行 $('#sortable2 li').find('#day').html('1');
时,您会丢失找到的 jquery 对象。您需要再次将其包装在 $() 中。这是无需使用查找的更简单方法。
$("#sortable2 #day").html(1)
这是一个工作示例http://jsfiddle.net/9nutmuvm/
$('#sortable2 li').find('#day')
这将创建一个包含所有匹配对象的集合,使用 .get(index) 检索特定对象。 http://api.jquery.com/get/
$('#sortable2 li').find('#day').get(i).html('1');
为了避免在每次迭代时重建集合,我会将其存储在循环外的变量中。
//Add all the hrs together to get the total
var $dayFields = $('#sortable2 li').find('#day');
for (var i in hrsArray) {
total += hrsArray[i];
//alert(list[i].toString());
//Object found at this point, need to figure out how to send text to day span in it.
if (total / 7.5 <= 1) {
$($dayFields.get(i)).html('1');
}
}
编辑:
解决这个问题的更好方法是遍历每个 li 而不是小时数组:
$("#sortable2 li").each(function() {
$(this).find("hrsSpan"); // This selects the hours field
$(this).find("day"); // This selects the day field in the same li
});