假设我没有<img src="x" />
我希望显示<img src="y">
是否可以这样做?
编辑:
抱歉,如果这个问题令人困惑...... 问题是这样的:当用户插入网址并且该网址是指向youtube的链接时,则需要显示youtube缩略图。如果该网址不是youtube视频的链接,则需要显示post.image(默认图片)。但是post.image没有显示出来!//显示post.image
以上两个显示图像正常。 但如果{{post.thumbnail}}为none,我想显示{{post.image}} ...我该怎么做?
这就是我需要它的原因,请参阅下面的代码
<td>
{% if post.main_image %} //if post has main_image
<img src="{{post.get_image_url}}" class="img-rounded" alt="☺" height="75" width="75"/>//display that image
{% elif post.url %}//if post has url
{% video post.url as my_video %} //setting post.url as my_video(problem starts here I think)
{% if my_video %} //if my_video is there
<img src="{{ my_video.thumbnail }}" class="img-rounded" alt="☺" height="75" width="75"/> //display the video thumbnail
{% else %} //if my_video isn't there and post.url isn't a link to youtube
<img src="{{post.image}}" class="img-rounded" alt="☺ " height="75" width="75"/> //display post.image
{% endif %}
{% endvideo %}
{% else %}
<img src="{{post.thumbnail}}" class="img-rounded" alt="☺" height="75" width="75"/>
{% endif %}
</td>
so main thing to look at it is for post.url if I don't have <img src="{{ my_video.thumbnail }}" class="img-rounded" alt="☺" height="75" width="75"/> I want
<img src="{{post.image}}" class="img-rounded" alt="☺ " height="75" width="75"/> //display post.image
答案 0 :(得分:0)
你可以在你的html文件中编写javascript并创建一个函数。只需用脚本标签包围您的javascript,就像这样......
<script>
function show_image(src, width, height, alt, src2, width2, height2, alt2) {
var img = document.createElement("img");
img.src = src;
img.width = width;
img.height = height;
img.alt = alt;
document.createElement("otherImg");
otherImg.src = src2;
otherImg.width = width2;
otherImg.heigh = height2;
otherImg.alt = alt2;
if(img.src.length === 0){
document.body.appendChild(img);
}else{
document.body.appendChild(img);
}
然后在某事上调用该函数。
<button onclick=
"show_image('foo.png',
100,
100,
'foo',
'bar.png,
'100',
'100',
'bar',
);">See Image</button>