Javascript 如何在html中使用if和else,是否可能?
比如说,如果我没有Javascript 如何在html中使用if和else,是否可能?,javascript,html,Javascript,Html,比如说,如果我没有我想显示,这可以吗 编辑: 对不起,如果这个问题让人困惑。。。 问题在于:当用户插入url时,如果该url是指向youtube的链接,则需要显示youtube缩略图。如果该url不是youtube视频的链接,则需要显示post.image(默认图像)。但是post.image无法显示 //显示post.image 以上两幅图像显示效果良好。 但是如果{post.thumbnail}}是none,我想显示{{post.image}。。。我该怎么做 这就是为什么我需要它,请看下面
我想显示
,这可以吗
编辑:
对不起,如果这个问题让人困惑。。。
问题在于:当用户插入url时,如果该url是指向youtube的链接,则需要显示youtube缩略图。如果该url不是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
{%if post.main_image%}//if post有main_image
//显示该图像
{%elif post.url%}//如果post有url
{%video post.url作为我的视频%}//将post.url设置为我的视频(我想问题从这里开始)
{%if my_video%}//如果我的_video在那里
//显示视频缩略图
{%else%}//如果我的视频不在那里,并且post.url不是youtube的链接
//显示post.image
{%endif%}
{%endvideo%}
{%else%}
{%endif%}
所以,若我并没有我想要的,那个么主要要看的是post.url
//显示post.image
您可以在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);
}
函数show_image(src、width、height、alt、src2、width2、height2、alt2){
var img=document.createElement(“img”);
img.src=src;
img.width=宽度;
img.height=高度;
img.alt=alt;
document.createElement(“其他img”);
otherImg.src=src2;
其他宽度=宽度2;
其他高度=高度2;
otherImg.alt=alt2;
if(img.src.length==0){
文件.正文.附件(img);
}否则{
文件.正文.附件(img);
}
然后调用某个函数
<button onclick=
"show_image('foo.png',
100,
100,
'foo',
'bar.png,
'100',
'100',
'bar',
);">See Image</button>
参见图片
你的意思是,如果图像无法加载,即它不存在于该位置/url等,那么请加载另一个图像。@adeneo no不使用alt。如果它存在或不存在,这就是我要做的事情。HTML是无逻辑的。你必须使用JavaScript来实现这一点。@Vohuman-hmm在JavaScript中这是多么令人困惑,你的意思是如果没有标记特定的src
属性存在于DOM中,然后插入一些其他标记?您好,我编辑了这个问题,您能告诉我是否可以使用上面的内容吗?不尝试制作任何按钮…如果可能,只想简单地显示图像…谢谢您的帮助