2017-08-08 59 views
1
<html> 
    <body> 
     <div class="hdd"> 
      <div class="hdd_Left"> 
       A 
      </div> 
      <div class="hdd_Center"> 
       B 
      </div> 
      <div class="hdd_Right"> 
       C 
      </div> 
     </div> 
    </body> 
</html> 

我想用叫可变如何通过一个类名内部类调用一个元素,并用它

<script> 
    $(".hdd").each(function() { 
     $(this).fadeTo(500,1); 
     $(this).getElementsByClassName("hdd_Left").animate({'margin-right': '100px'},"slow"); //doesn't work 
    } 
</script> 

$(this).getElementsByClassName("hdd_Left").animate({'margin-right': '100px'},"slow");

此行不起作用。

谢谢

回答

2

你实际上应该写。不要混合Vanilla JS和Jquery。

$(this).find(".hdd_Left").animate({'margin-right': '100px'},"slow"); 
+0

谢谢你,这太棒了! –

0

您面临的问题是该对象上没有getElementByBlassName方法调用。

Actual error when calling getElementByBlassName

相反,你需要使用类似:

 $(document).ready(function(){   
      $(".hdd").each(function() { 
       $(this).fadeTo(500,1); 
       var childrenItems = $(this).children(".hdd_Left"); 
       childrenItems.animate({'margin-right': '100px'},"slow"); //works fine 
      }) 
     }); 

由于上面的代码工作100%,但并没有做太多我的屏幕上,我增加了一个额外的方法来显示你在jquery中做的另一种方式,但在所有子项目上有一些动画:

 $(document).ready(function(){   
      $('.hdd > div').each(function() { 
       $(this).animate({ 
       width: "70%", 
       opacity: 0.4, 
       marginLeft: "0.6in", 
       fontSize: "3em", 
       borderWidth: "10px" 
       }, 1500); 
      }) 
     }); 
相关问题