我正在制作一个基于文本的小游戏,我试图通过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