使用javascript中的if和else语句来显示图像(如果图像不存在,则显示其他图像)

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

本文关键字:图像 显示 不存在 如果 其他 javascript 显示图 if else 语句 使用      更新时间:2024-05-02

您好,是否可以将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'; }

使用相同的图像将使用正确的位置。