2016-12-30 95 views
0

你好精彩的人,更新Src屬性更改後的圖像

我試圖讓我更改'src'屬性後更新圖像。我試圖用日期戳附加URL,但這似乎不起作用。有人可以告訴我我要去哪裏錯,或者只是建議一個更好的方法來一起做這件事?

我已經包含了我的整個代碼,因爲我不知道問題出在哪裏。恐怕我有點兒不知情。 'displayOut'函數的前三行很可能出現問題所在。預先感謝您的幫助。

var db = [{ // ROOMS 
 
    rooms: [{ // Room 0 - North room 
 
     description: "You awake to find yourself in a dank smelling old room, plaster and smashed glass litters the floor. To the North is a broken window, beyond which you can only see a thick grey mist. There is one door by which to exit, to the South.", 
 
     roomImg: "images/room_0.jpg", 
 
     exits: { 
 
     north: false, 
 
     south: 1, 
 
     east: false, 
 
     west: false, 
 
     up: false, 
 
     down: false 
 
     }, 
 
     roomInvent: ["a Box of Matches", "a Glass Shard"] 
 
    }, { // Room 1 - Corridor 
 
     description: "You are in a short, dark corridor, a single tungsten bulb hangs stiffly from the ceiling. There is a light switch on the wall.", 
 
     roomImg: "images/room_1.jpg", 
 
     exits: { 
 
     north: 0, 
 
     south: 4, 
 
     east: 3, 
 
     west: false, 
 
     up: 5, 
 
     down: false 
 
     }, 
 
     roomInvent: [] 
 
    }, { // Room 2 - West Room - Locked room 
 
     description: "", 
 
     roomImg: "images/room_2.jpg", 
 
     exits: { 
 
     north: false, 
 
     south: false, 
 
     east: false, 
 
     west: false, 
 
     up: false, 
 
     down: false 
 
     }, 
 
     roomInvent: [] 
 
    }, { // Room 3 - East room - Bedroom 
 
     description: "You are in the Bedroom", 
 
     roomImg: "images/room_3.jpg", 
 
     exits: { 
 
     north: false, 
 
     south: false, 
 
     east: false, 
 
     west: 1, 
 
     up: false, 
 
     down: false 
 
     }, 
 
     roomInvent: [] 
 
    }, { // Room 4 - South room - kitchen 
 
     description: "You are in a small kitchen. There is an old log fire on the East wall, and a door leading outside to the South.", 
 
     roomImg: "images/room_4.jpg", 
 
     exits: { 
 
     north: 1, 
 
     south: false, 
 
     east: false, 
 
     west: false, 
 
     up: false, 
 
     down: false 
 
     }, 
 
     roomInvent: [] 
 
    }, { // Room 5 - Attic 
 
     description: "You are in the Attic.", 
 
     roomImg: "images/room_5.jpg", 
 
     exits: { 
 
     north: false, 
 
     south: false, 
 
     east: false, 
 
     west: false, 
 
     up: false, 
 
     down: 1 
 
     }, 
 
     roomInvent: [] 
 
    }] 
 
    }, // End Rooms 
 
    { // ITEMS 
 
    items: [{ 
 
     itemIndex: 0, 
 
     name: "a Box of Matches", 
 
     useWith: null, 
 
     examine: "There is only a single match inside." 
 
    }, { 
 
     itemIndex: 1, 
 
     name: "a Glass Shard", 
 
     useWith: null, 
 
     examine: "It looks sharp" 
 
    }, { 
 
     itemIndex: 2, 
 
     name: "a Mallet", 
 
     useWith: null, 
 
     examine: "It is old and rusty, but otherwise uninteresting." 
 
    }] 
 
    } 
 

 
]; //End db 
 

 
var inventory = []; 
 
var inputTextBox = document.getElementById("inputTextBox"); 
 
var diologueBox = document.getElementById("diologueBox"); 
 
var strOut = ""; 
 
var roomLoc = 0; 
 

 

 

 
function displayOut() { 
 
    // images 
 
    let dt = new Date; 
 
    document.getElementById("imgBox").setAttribute("src", db[0].rooms[roomLoc].roomImg + "?dt=" + dt.getTime()); 
 
    // Diologue box 
 
    diologueBox.innerHTML = db[0].rooms[roomLoc].description + 
 
    (function() { // Check if room has items in inventory, if so, list them. 
 
     if (db[0].rooms[roomLoc].roomInvent != "") { 
 
     return "<br><br> The room contains " + 
 
      (function() { 
 
      let items = ""; 
 
      for (let i = 0; i < db[0].rooms[roomLoc].roomInvent.length; i++) { 
 
       items += db[0].rooms[roomLoc].roomInvent[i] + ", "; 
 
      }; 
 
      items = items.slice(0, items.length - 2); 
 
      return items; 
 
      })(); 
 
     } else { 
 
     return "<br><br> The room is empty "; 
 
     }; 
 
    })(); 
 
}; 
 
// Function for changing room location 
 
function navigateTo(direction) { 
 
    if (db[0].rooms[roomLoc].exits[direction] === false) { 
 
    outputBox.innerHTML = "You cannot go " + direction + " from here." 
 
    } else { 
 
    roomLoc = db[0].rooms[roomLoc].exits[direction]; 
 
    displayOut(); 
 
    } 
 
} 
 

 
inputTextBox.addEventListener("keypress", function(event) { 
 
    let x = event.which || event.keyCode; 
 
    if (x === 13) { // 13 is the Return key 
 
    switch (inputTextBox.value.toLowerCase()) { 
 
     //Diologue Navigation 
 
     case "": 
 
     // Nothing happens 
 
     break; 
 
     case "north": 
 
     case "n": 
 
     navigateTo("north"); 
 
     break; 
 
     case "south": 
 
     case "s": 
 
     navigateTo("south"); 
 
     break; 
 
     case "east": 
 
     case "e": 
 
     navigateTo("east"); 
 
     break; 
 
     case "west": 
 
     case "w": 
 
     navigateTo("west"); 
 
     break; 
 
     case "up": 
 
     case "u": 
 
     navigateTo("up"); 
 
     break; 
 
     case "down": 
 
     case "d": 
 
     navigateTo("down"); 
 
     break; 
 
     //Dioogue Help 
 
     case "help": 
 
     outputBox.innerHTML = " Here is a list of useful commands: North, South, East, West, Up, Down, Look, Examine, Inventory, Take, Use"; 
 
     break; 
 
     // 
 
     default: 
 
     outputBox.innerHTML = " I have no idea what " + "'" + inputTextBox.value.bold() + "'" + " means..."; 
 
    } // End switch 
 

 
    //Clear InputTextBox 
 
    inputTextBox.value = ""; 
 
    inputTextBox.setAttribute("placeholder", ""); 
 

 

 
    } // End KeyPress 
 
}); // End Event listener 
 

 
displayOut();
@charset "utf-8"; 
 
@font-face { 
 
    font-family: 'Terminal'; 
 
    /*a name to be used later*/ 
 
    src: url(lcd_solid.ttf); 
 
    /*URL to font*/ 
 
} 
 
* { 
 
    font-family: Terminal; 
 
    font-size: 18px; 
 
    margin: 0; 
 
    border: 0; 
 
} 
 
body, 
 
html { 
 
    font-family: helvetica; 
 
    font-size: 12px; 
 
    background: #282828; 
 
} 
 
#imgBox { 
 
    margin: 0px auto 0px auto; 
 
    background-image: url("../images/room_0.jpg"); 
 
    background-repeat: no-repeat; 
 
    height: 600px; 
 
    width: 1024px; 
 
} 
 
#conBox { 
 
    margin: 0px auto 0px auto; 
 
    position: relative; 
 
    width: 1024px; 
 
    height: 300px; 
 
} 
 
#diologueBox { 
 
    background: #CCC; 
 
    height: 200px; 
 
    clear: both; 
 
    padding: 1px 0px 1px 3px; 
 
    overflow: none; 
 
    position: relative; 
 
} 
 
#diologueBox p { 
 
    margin: 5px; 
 
    left: 0px; 
 
    bottom: 0px; 
 
} 
 
#outputBox { 
 
    background: #CCC; 
 
    height: 50px; 
 
    clear: both; 
 
    padding: 1px 0px 1px 3px; 
 
    overflow: none; 
 
    position: relative; 
 
} 
 
#inputBox { 
 
    position: relative; 
 
    height: 20px; 
 
    background: #C1C1C1; 
 
} 
 
#inputTextBox { 
 
    height: 18px; 
 
    padding: 1px; 
 
    float: right; 
 
    width: 1004px; 
 
    background: #C1C1C1; 
 
} 
 
::-webkit-input-placeholder { 
 
    color: red; 
 
} 
 
:-moz-placeholder { 
 
    /* Firefox 18- */ 
 
    color: red; 
 
} 
 
::-moz-placeholder { 
 
    /* Firefox 19+ */ 
 
    color: red; 
 
} 
 
:-ms-input-placeholder { 
 
    color: red; 
 
} 
 
#inputTextBox.focus, 
 
input:focus { 
 
    outline: none; 
 
} 
 
#bullet { 
 
    width: 15px; 
 
    float: left; 
 
    padding: 4px 0px 1px 3px; 
 
}
<!DOCTYPE html> 
 
<html lang="en"> 
 

 
<head> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
    <link rel="stylesheet" type="text/css" href="styles/style.css"> 
 
    <meta charset="utf-8"> 
 
    <title>Untitled</title> 
 
</head> 
 

 
<body> 
 
    <div id="imgBox"></div> 
 
    <div id="conBox"> 
 
    <div id="diologueBox"></div> 
 
    <div id="outputBox"></div> 
 
    <div id="inputBox"> 
 
     <div id="bullet">></div> 
 
     <input id="inputTextBox" type="text" maxlength="60" placeholder="Type commands here, type 'Help' at any time for list of commands" autofocus></input> 
 
    </div> 
 
    </div> 
 

 
    <script src="script2.js"></script> 
 

 
</body> 
 

 
</html>

+0

我剛開始使用這兩個「讓」和「常量」,它只是一個確保正確的方法範圍。我知道我沒有必要把它放在哪裏,但我認爲這對我未來的發展是一個很好的做法。 – Cuckoo

回答

1

快速的解決方案:使用背景圖片,你已經在你的CSS做。

編輯:但是更改src屬性應該可以工作,我不確定jquery如何處理它。

如果使用的背景是不是一種選擇,你可以:

  • 已經在你的HTML所有<img>和隱藏/顯示他們(快,但它需要加載的一切前期)
  • 創建IMG與$("<img/>")飛行中的元素,並取代舊的。鑑於你的使用情況,這是相當便宜的資源明智。
  • 用帆布畫你的圖片(但當時爲什麼不使用的background-image?)
+0

謝謝!我知道我過度思考這個問題,我已經使用了'background-img'選項,它可以起到一定的作用。謝謝! :) – Cuckoo

+0

Canvas和可以右鍵點擊保存,背景圖片沒那麼多 – Noino