2016-04-10 52 views
1
<div class="image"> 
<img id="img-1" src="https://cdn0.vox-cdn.com/images/verge/default-avatar.v9899025.gif" /> 
<img id="img-2" src="https://cdn0.vox-cdn.com/images/verge/default-avatar.v9899025.gif" /> 
<img id="img-5" src="https://cdn0.vox-cdn.com/images/verge/default-avatar.v9899025.gif" /> 
<img id="img-15" src="https://cdn0.vox-cdn.com/images/verge/default-avatar.v9899025.gif" /> 
<img id="img-25" src="https://cdn0.vox-cdn.com/images/verge/default-avatar.v9899025.gif" /> 
<img id="img-35" src="https://cdn0.vox-cdn.com/images/verge/default-avatar.v9899025.gif" /> 
<img id="img-50" src="https://cdn0.vox-cdn.com/images/verge/default-avatar.v9899025.gif" /> 
</div> 

$(".image img").each(function() { 
     var $this = $(this), 
      getClass = $this.attr('img') 
     splitClass = $this.prop('id').split("-") 
     if (splitClass[1] <= 20) { 
      $this.attr("src", "http://lorempicsum.com/futurama/350/200/1"); 
      this.nextElementSibling.remove(); 

     } else if (splitClass[1] >= 21) { 
      $this.attr("src", "http://www.top13.net/wp-content/uploads/2014/11/4-small-flowers.jpg"); 
      this.nextElementSibling.remove(); 

     } else { 
      $this.attr("src", "https://cdn0.vox-cdn.com/images/verge/default-avatar.v9899025.gif"); 
     } 
    }); 

>如何使用>,<,< =,> = =在jQuery中工作? 默認img是default-avatar.gif。 我想(splitClass[1] >= 21)與分鐘IMG變化爲21,最大爲40 ,然後....如何使用邏輯運算符JavaScript或jQuery

else { 
     $this.attr("src", "https://cdn0.vox-cdn.com/images/verge/default-avatar.v9899025.gif"); 
      } 

將默認avatar.gif工作,因爲我不改變它在jQuery的。

+3

運營商是無關的jQuery。如果您的值爲'23',那麼您需要parseInt('23')並以數字形式工作,並且您可以應用操作員。 http://www.w3schools.com/jsref/jsref_parseint.asp – daremachine

+0

'getClass = $ this.attr('img')'的預期結果是什麼? – guest271314

+0

@ guest271314真的嗎?我認爲JavaScript不是動態類型化語言,並且您總是需要將字符串轉換爲運算符的數字。 – daremachine

回答

1

儘量在.each()循環內用.hide()代替.remove()。您還可以將className添加到display:none設置爲css的元素,以在.each()處迭代後從document中選擇和刪除元素。

$(".image img").each(function() { 
 
    var $this = $(this), 
 
    getClass = $this.attr('img') 
 
    splitClass = $this.prop('id').split("-") 
 
    if (splitClass[1] <= 20) { 
 
    $this.attr("src", "http://lorempicsum.com/futurama/350/200/1"); 
 
    $this.next().hide(); // .addClass("toRemove") 
 
    console.log("less than or equal to 20", splitClass[1] <= 20) 
 
    } else if (splitClass[1] >= 21) { 
 
    $this.attr("src", "http://www.top13.net/wp-content/uploads/2014/11/4-small-flowers.jpg"); 
 
    $this.next().hide(); // .addClass("toRemove") 
 
    console.log("greater than or equal to 20", splitClass[1] >= 21) 
 
    } else { 
 
    $this.attr("src", "https://cdn0.vox-cdn.com/images/verge/default-avatar.v9899025.gif"); 
 
    } 
 
}); // .filter(".toRemove").remove()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<div class="image"> 
 
    <img id="img-1" src="https://cdn0.vox-cdn.com/images/verge/default-avatar.v9899025.gif" /> 
 
    <img id="img-2" src="https://cdn0.vox-cdn.com/images/verge/default-avatar.v9899025.gif" /> 
 
    <img id="img-5" src="https://cdn0.vox-cdn.com/images/verge/default-avatar.v9899025.gif" /> 
 
    <img id="img-15" src="https://cdn0.vox-cdn.com/images/verge/default-avatar.v9899025.gif" /> 
 
    <img id="img-25" src="https://cdn0.vox-cdn.com/images/verge/default-avatar.v9899025.gif" /> 
 
    <img id="img-35" src="https://cdn0.vox-cdn.com/images/verge/default-avatar.v9899025.gif" /> 
 
    <img id="img-50" src="https://cdn0.vox-cdn.com/images/verge/default-avatar.v9899025.gif" /> 
 
</div>

+1

@KarlFriedchicken請注意,'img'元素似乎沒有名爲'img'的屬性; getClass = $ this.attr('img')'的目的是什麼? – guest271314