JavaScript、if文で画像の切り分けがうまくできません。
現在N予備校のプログラミングコースの問題を写経し、時間当てゲームを作成しました。
アレンジとして画像を追記したいのですがうまくいきません。
流れ
0. 番号リスト流れとしてhtmlのIDをJSで取得
0. if文で画像を呼び出し
サンプルとしてダミー画像で試しているのですが、うまくいきません。
コンソーるをチェックしてもエラーが出ていません。
原因わかる方いましたら、他のやり方でもいいので教えていただけると助かります。
よろしくお願いします。
html
1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4 <meta charset="UTF-8"> 5 <title>JSオブジェクト</title> 6</head> 7<body> 8 <h1 id="display-area"></h1> 9 <p id="trueImg"></p> 10 <p id="falseImg"></p> 11 <script src="object_game.js"></script> 12</body> 13 14<script> 15(function () { 16 'use strict'; 17 var game = { 18 startTime: null, 19 displayArea: document.getElementById('display-area') 20 }; 21 22 // 画像変数 23 var trueImg = document.getElementById('trueImg'); 24 var falseImg = document.getElementById('falseImg'); 25 26 function start() { 27 game.startTime = new Date().getTime(); 28 document.body.onkeypress = stop; 29 } 30 31 function stop() { 32 var currentTime = new Date().getTime(); 33 var seconds = (currentTime - game.startTime) / 1000; 34 // 9.5 秒から 10.5 秒の間の処理 35 if (9.5 <= seconds && seconds <= 10.5) { 36 game.displayArea.innerText = seconds + '秒でした。すごい。'; 37 // なぜか呼び出されない 38 trueImg.src = "https://placehold.jp/150x150.png"; 39 } else { 40 game.displayArea.innerText = seconds + '秒でした。残念。'; 41 // なぜか呼び出されない 42 falseImg.src = "https://placehold.jp/150x150.png"; 43 } 44 } 45 46 if (confirm('OKを押して10秒だと思ったら何かキーを押してください。')) { 47 start(); 48 } 49 50}) (); 51</script> 52</html>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/28 06:31