gpt4 book ai didi

jquery-selectors - JQuery 每个循环 - 对当前循环的元素执行某些操作

转载 作者:行者123 更新时间:2023-12-01 02:47:47 26 4
gpt4 key购买 nike

我有以下 .each jQuery 循环。

$('.category').each(function(index) {
if( $('.category > span').parent().next('h2') ) {

// get currently looped element which follows the condition
}

});

如何通过if语句中的.each获取当前循环元素?

最佳答案

How to get currently looped element through .each inside the if statement?

用途:

$(this) // represents current iterated element in wrapped set

示例:

$('.category').each(function(index) {
if( $('.category > span').parent().next('h2') ) {
$(this).css('color', 'red');
}
});

请注意,您还可以使用 this 关键字获取 DOM 对象而不是 jQuery 对象。

关于jquery-selectors - JQuery 每个循环 - 对当前循环的元素执行某些操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7346295/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com