我正在製作一個基於文本的小遊戲,我試圖通過Youtube嵌入合併一些背景音樂。也許不是最好的解決辦法,但我的目標,它的工作原理:)innerHTML不能在if/else中工作
我有一個JavaScript數據庫建立多個數據收集,建立這樣的:
{
"question":"blabla.",
"answers":[
{"title":"Left","response":3},
{"title":"Right","response":2}
],
"youembed":"<iframe id='youtube' width='100' height='100' src='http://www.youtube.com/embed/VIIOVtWGSj8?autoplay=1' frameborder='0' allowfullscreen></iframe>"
},
我把從數據庫中的內容我的HTML與下面的一段:
document.getElementById("question").innerHTML = db[currentLocation].question;
但是,我只希望youembed
實際上放置在HTML,如果db[currentLocation].youembed
長度超過0.1
所以我寫了以下內容:
if (youembed > 0) {
document.getElementById("youembed").innerHTML = db[currentLocation].youembed;
}
每當我使用if語句的document[..].youembed
之外,它的工作原理沒有問題,但是,我希望它在IF語句工作,所以它不」 t取代當前正在播放的歌曲,直到db[currentLocation].youembed
> 0
我在做什麼錯?我有一個alert(youembed)
放置在代碼中,所以我可以看到長度,它確實顯示了一個嵌入的是0,另一個在頁面2是145,但它不運行if
-在這種情況下的陳述。
下面是完整的Javascript代碼:
var currentLocation = 0;
window.printCurrentLocation = function(){
document.getElementById("question").innerHTML = db[currentLocation].question;
var youembed = db[currentLocation].youembed;
alert(youembed.length);
if (youembed > 0) {
document.getElementById("youembed").innerHTML = db[currentLocation].youembed;
}
else {
}
var answers = "";
for(var i=0,l=db[currentLocation].answers.length;i<l;i++){
answers += "<p><button onclick='setLocation("+db[currentLocation].answers[i].response+")'>"+db[currentLocation].answers[i].title+"</button></p>";
}
document.getElementById("answers").innerHTML = answers;
}
window.setLocation = function(num) {
currentLocation = num;
window.printCurrentLocation();
}
window.printCurrentLocation();
我忘了補充一點,我設置了一個變量'youembed'。請參閱添加的Javascript代碼! :) – Rvervuurt 2014-12-05 11:06:01
@Rvervuurt - 是的我看到了,回答更新 – Jamiec 2014-12-05 11:06:27
哦,男人,當然我也應該檢查if語句的長度! :/ 感謝:D – Rvervuurt 2014-12-05 11:08:05