sách gpt4 ăn đã đi

Chỉ mục jQuery .each()?

In lại 作者:IT王子 更新时间:2023-10-29 03:25:19 29 4
mua khóa gpt4 giày nike

tôi đang sử dụng

$('#list option').each(function(){
//làm gì đó
});

循环列表中的选项。我想知道如何获取当前循环的索引?

因为我不想让 var i = 0;循环内部有 i++;

câu trả lời hay nhất

$('#list option').each(function(index){
//làm gì đó
console.log(index);
});

记录索引:)

下面是一个更详细的例子。

function run_each() {

var $results = $(".results");

$results.empty();

$results.append("==================== START 1st each ====================");
console.log("==================== START 1st each ====================");

$('#my_select option').each(function(index, value) {
$results.append("
");
// log the index
$results.append("index: " + index);
$results.append("
");
console.log("index: " + index);
// logs the element
// $results.append(value); this would actually remove the element
$results.append("
");
console.log(value);
// logs element property
$results.append(value.innerHTML);
$results.append("
");
console.log(value.innerHTML);
// logs element property
$results.append(this.text);
$results.append("
");
console.log(this.text);
// jquery
$results.append($(this).text());
$results.append("
");
console.log($(this).text());

// BEGIN just to see what would happen if nesting an .each within an .each
$('p').each(function(index) {
$results.append("==================== nested each");
$results.append("
");
$results.append("nested each index: " + index);
$results.append("
");
console.log(index);
});
// END just to see what would happen if nesting an .each within an .each

});

$results.append("
");
$results.append("==================== START 2nd each ====================");
console.log("");
console.log("==================== START 2nd each ====================");


$('ul li').each(function(index, value) {
$results.append("
");
// log the index
$results.append("index: " + index);
$results.append("
");
console.log(index);
// logs the element
// $results.append(value); this would actually remove the element
$results.append("
");
console.log(value);
// logs element property
$results.append(value.innerHTML);
$results.append("
");
console.log(value.innerHTML);
// logs element property
$results.append(this.innerHTML);
$results.append("
");
console.log(this.innerHTML);
// jquery
$results.append($(this).text());
$results.append("
");
console.log($(this).text());
});

}



$(document).on("click", ".clicker", function() {

run_each();

});
.results {
background: #000;
chiều cao: 150px;
tràn: tự động;
color: lime;
font-family: arial;
padding: 20px;
}

.container {
hiển thị: flex;
}

.one,
.two,
.three {
width: 33.3%;
}

.one {
background: yellow;
căn chỉnh văn bản: ở giữa;
}

.two {
background: pink;
}

.three {
background: darkgray;
}









  • canada

  • america

  • france





do


re


me












关于jQuery .each() 索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4341977/

29 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress