我有一个图像 slider ,可以滑动带有新图像的 div,并将新 div 的 css 属性设置为 display:block; z 索引:5;。它还设置了带有不可见图像的div以显示:none;和 z 索引:0;
我现在只想向满足条件 z-index:5 的当前 image-div 添加一个类;
我已经尝试了以下代码,但 $(this) 不起作用,而且我不知道如何仅添加当前满足条件的类。
if ($(".slides_control").children().css( "z-index", "5" )){
$(this).addClass("test");
} else{
$(this).removeClass("test");
}
<div class="slides_control">
Container div
<div>div with the first image</div>
<div>div with the second image</div>
<div>div with the second image</div>
</div>
最佳答案
你可以这样做
var arr = $(".slides_control").children().filter(function() {
return $(this).css("z-index") == "5"
}).addClass('test');
关于javascript - 无法让简单的 if 循环与 $(this) 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26359764/