代码之家  ›  专栏  ›  技术社区  ›  G. Ball.

无法淡出数组中的特定元素

  •  0
  • G. Ball.  · 技术社区  · 6 年前

    我相信这真的很容易,我只是不理解。但我试图以jquery选择器的第一个元素为目标,并淡出该元素。我可以淡出两个元素,但不能使用索引分别淡出其中任何一个。我一直得到一个“jQuery.Deferred异常:$(…)[0]。淡出不是一个函数错误。我正在使用codepen,所以jquery库和SCS被加载到其中。

    HTML

    <div class="container">
      <img src="https://static.pexels.com/photos/371633/pexels-photo-371633.jpeg" class="container__image">
      <img src="https://static.pexels.com/photos/414171/pexels-photo-414171.jpeg" class="container__image">
    </div>  
    

    CSS

    * {
      margin:0;
      padding: 0;
      box-sizing: border-box;
    
    }
    
    .container {
      width: 350px;
      height: 350px;
      background-color: blue;
      margin: 15px auto;
    
      &__image {
        max-width: 100%;
        height: auto;
      } 
    }
    

    Javascript:

    $(document).ready(function() {
      $(".container__image")[0].fadeOut();
    });  
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   4b0 Agit    6 年前

    你可以使用 eq

     $(document).ready(function() {
       $(".container__image").eq(0).fadeOut();
     });
    

    或者您可以使用 array 像这样

    $(document).ready(function() {
      var array = [];
      $('.container__image').each(function(i, li) {
        array.push($(li));
      });
      array[0].fadeOut();
    });