2016-04-12 8 views
0

私は<img src="x" />が表示されていないとしましょう<img src="y">を表示したいのですがこれは可能ですか?htmlでifやelseを使うにはどうしたらいいですか?

編集:質問が混乱している場合

申し訳ありません... 問題はこれです:ユーザーがURLを挿入すると、そのURLがユーチューブへのリンクである場合は、ユーチューブのサムネイルを表示する必要があります。そのURLがYouTube動画へのリンクでない場合、post.image(デフォルト画像)を表示する必要があります。しかし、post.imageは表示されません!

//表示post.image

上記の二つの画像を表示するの罰金。 {{post.thumbnail}}がnoneの場合、{{post.image}}を表示したいのですが...どうすればいいですか?私はそれを必要とする理由です

,,あなたは、あなたのHTMLファイルにJavaScriptを記述し、機能させることができ、以下のコード

<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

画像がロードできない、つまりその場所/ URLなどに画像が存在しない場合、代わりに別の画像を読み込むことを意味します。 – adeneo

+0

@adeneo noを使っていません。もし存在すれば、私がやろうとしているのですか。 –

+0

いいえ、HTMLは論理的ではありません。これにはJavaScriptを使用する必要があります。 – undefined

答えて

0

を参照してください。ただ、そのようなスクリプトタグを使用して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> 
+0

こんにちは、私はこのために上記を使用することができますか教えてください質問 –

+0

ボタン...可能であれば単純に画像を表示したい...助けてくれてありがとう –

関連する問題