简体   繁体   中英

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

Hello is it possible to use django template language and javascript together such as this way:

                {% 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 doesn't have a length. You can test img.width .

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

Using the same image will use the right place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM