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

标签 javascript django

您好,是否可以像这样一起使用 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'; }

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

关于javascript - 在javascript中使用if和else语句来显示图像(如果图像不存在则显示其他图像),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36567639/

相关文章:

javascript - 当响应式样式不再适用时删除类

javascript - AngularJS : Service object empty

python - {% trans "This is the title."%} 是做什么用的,我不明白这个 api

javascript - jQuery:如何跳转到 iframe 内表格中的特定行

javascript - 我如何在 Highchart 中使列和 y 轴之间有 1px 的空间?

javascript - Jquery 将所有引用的页面合并到根页面?

python - 如何从数据库中获取所有数据并显示在表格中

python - 使用大型旧版 MySQL 数据库时,我可以在多大程度上依赖 django 的spectrdb 命令?

python - Django,教程 - 错误 : No module named urls

python - 使用 django REST 进行 LDAP 身份验证