Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JQuery-如果元素具有类,则添加类_Jquery_Animation_Addclass - Fatal编程技术网

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

JQuery-如果元素具有类,则添加类,jquery,animation,addclass,Jquery,Animation,Addclass,我想将类添加到具有类“alignleft”的元素中。我的代码: $(document).ready(function () { if($('.foto').children().hasClass('alignleft')){ $(this).addClass('animated bounceInLeft'); } }); 它不起作用,我也不知道为什么。但是,如果我使用$'.foto'.children.addClass'animated bounceInLeft

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

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

它不起作用,我也不知道为什么。但是,如果我使用$'.foto'.children.addClass'animated bounceInLeft';它将类应用于类为“alignleft”的所有子元素。因此,$this maybe中存在问题?

您可以使用类选择器以元素为目标,然后向元素添加类:

$('.foto .alignleft').addClass('animated bounceInLeft');
您不能使用它,它引用窗口对象。而是使用类作为方法中的选择器

或使用

试一试

$document.readyfunction{ var elem=$'.foto.alignleft'; 如果元素{ 元素addClass“动画反弹左”; } }; .alignleft{颜色:红色;} .alignright{color:green;} .动画{背景:浅灰色;} A组 B组 C组 D组
$('.foto').children('.alignleft').addClass('animated bounceInLeft');
$('.foto > .alignleft').addClass('animated bounceInLeft');
$(document).ready(function() {
 var elem = $('.foto .alignleft');
  if (elem) {
    elem.addClass('animated bounceInLeft');
  }
});
$(document).ready(function() {
   $('.foto .alignleft').addClass('animated bounceInLeft');
});