簡體   English   中英

在javascript中使用if和else語句顯示圖像(如果圖像不存在,則顯示其他圖像)

[英]using if and else statement in javascript to display image (if image doesn't exists display other image)

您好,可以像下面這樣一起使用django模板語言和javascript:

                {% if post.main_image %}
<img src="{{post.get_image_url}}"  class="img-rounded" alt="☺" height="75" width="75"/>
  {% elif post.url %}
  {% video post.url as my_video %}
        <img src="{{ my_video.thumbnail }}" id="myThumb" class="img-rounded" alt="☺" height="75" width="75"/>
{% endvideo %}
{% else %}
<img src="{{post.thumbnail}}"  class="img-rounded" alt="☺" height="75" width="75"/>
{% endif %}



<script>
$(document).ready(function() {
    var i = $('mythumb');   // using an id  <img id='mythumb'.....
    if( i.width === 0 ) i.src='<img src="{{post.image}}"  class="img-rounded" alt="☺ EBAGU" height="75" width="75"/>';
}


</script>

img沒有長度。 您可以測試img.width

$(document).ready(function() { var i = $('mythumb'); // using an id <img id='mythumb'..... if( i.width === 0 ) i.src='y'; }

使用相同的圖像將使用正確的位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM