gpt4 book ai didi

JQuery - 如果元素有类则添加类

转载 作者:行者123 更新时间:2023-12-01 01:22:00 25 4
gpt4 key购买 nike

我想向具有“alignleft”类的元素添加类。我的代码:

$(document).ready(function () {
if($('.foto').children().hasClass('alignleft')){
$(this).addClass('animated bounceInLeft');
}
});

它不起作用,我不知道为什么。但是,如果我使用 $('.foto').children().addClass('animatedounceInLeft'); ,它将类应用于类为“alignleft”的所有子元素。那么问题可能出在 $(this) 中?

最佳答案

您不能使用 this,它引用窗口对象。而是使用类作为 children() 中的选择器方法。

$('.foto').children('.alignleft').addClass('animated bounceInLeft');

或者使用child selector(>)

$('.foto > .alignleft').addClass('animated bounceInLeft');

关于JQuery - 如果元素有类则添加类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37938500/

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