繁体   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