本文介绍了如果$(element)hasClass那么.animate()不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个元素有一个类activeSlide,由于某种原因,它不能正常工作的动画动画的箭头。我不知道为什么这是,任何人都可以提供一个小洞察到什么做错了?

  $(document).ready (function(){
if($('#navitem-2')。hasClass(activeSlide)){
$(#navarro)。animate({marginLeft:220px ,500);
};
});


$ b

谢谢!



更新 * :以下是类别在点击时更改的示例,但动画不起作用:

解决方案
  $(document).ready(function()
{
if($('#navitem-2 .activeSlide')。length> 0)
{
$(#navarro)。animate({marginLeft:220px},500);
}
}
pre>

这应该在条件下工作100%




  • #navitem-2 现在

  • class activeSlide #navitem-2

  • #navarro 存在。



如果您有控制台(例如Google Chrom Developer Tools),您可以在JavaScript中添加一些记录机制

  $。fn.log = function()
{
if(LoggingEnabled&&控制台&& console.log)
{
console.log(this);
}
return this;
}

然后尝试:

  LoggingEnabled = true; 
$(document).ready(function()
{
var Check = $('#navitem-2 .activeSlide')log();
if长度> 0)
{
$(#navarro)。log()。animate({marginLeft:220px},500);
}
} b $ b LoggingEnabled = false;

,您可以看到日志控制台中显示的内容。


Im working on animating an arrow when an element has a class of "activeSlide" for some reason it is not functioning as expected. Im not sure why this is, can anyone provide a little insight in to what im doing wrong?

$(document).ready(function() {
 if($('#navitem-2').hasClass("activeSlide")){
  $("#navarro").animate({marginLeft:"220px"}, 500);
 };
});

Thanks!

*Update*: Here is an examaple where the classes change onclick, but the animation does not function: http://jsfiddle.net/somewhereinsf/pn5sx/1/

解决方案
$(document).ready(function()
{
    if($('#navitem-2 .activeSlide').length > 0)
    {
         $("#navarro").animate({marginLeft:"220px"}, 500);
    }
}

This should work 100% under the conditions

  • #navitem-2 exists
  • class activeSlide is a child of #navitem-2
  • #navarro exists.

if you have a console such as Google Chrom Developer Tools you can add some logging mechanism in your javascript

$.fn.log = function()
{
    if(LoggingEnabled && console && console.log)
    {
        console.log(this);
    }
    return this;
}

And then try:

LoggingEnabled  = true;
$(document).ready(function()
{
    var Check = $('#navitem-2 .activeSlide').log();
    if(Check.length > 0)
    {
         $("#navarro").log().animate({marginLeft:"220px"}, 500);
    }
}
LoggingEnabled = false;

and you can see what's appearing in the log console.

这篇关于如果$(element)hasClass那么.animate()不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 16:39