JavaScript 未显示正确的图像

标签 javascript jquery

我有一个简单的代码,可以在夜间显示一张图像,在白天显示一张图像。由于某种原因,白天图像总是显示。

JavaScript 有点 split 。这部分位于页面上:

<script>

//waits until document is open

$(window).load(function() {

//if it's night time when you read this it displays a night time photo, if it's day time it's a day time photo... don't know how many people might notice

if (time === "night"){
 $.backstretch("cafenight.jpg");

 //makes the font white with a black outline so you can read it easily on the night photo
  $("#content").css("color","#FFFFFF");
    $("#content").css("text-shadow","1px 0 0 #000, 0 -1px 0 #000, 0 1px 0 #000, -1px 0 0     #000");

}

else {
 $.backstretch("cafeday.jpg");

    $("#content").css("text-shadow","1px 0 0 #fff, 0 -1px 0 #fff, 0 1px 0 #fff, -1px 0 0      Ωfff");}});

 $(document).ready(function(){

 //fades in the first line of text

$("#partFive").fadeIn(2000)

setTimeout(function () {

//fades in the second line of text

$("#partFive").fadeOut(2000)

setTimeout(function () {


$("#partSix").fadeIn(3000)

setTimeout(function () {


$("#partSix").fadeOut(3000)


setTimeout(function () {

window.location.href = "6.html";

    }, 4000);

    }, 2000);

    }, 2000);

    }, 3000);

});

</script>

这部分位于basic2.js

// Javascript and Jquery rock!
//Variables to be used throughout
//The user's current hour

var hour = (new Date()).getHours()

//use the variable 'time' to know if it is day or night. Can use to change background even wording.
//The variable 'time' will either be Day or Night depending on what time it is

var time = []

if (hour < 20){
 time.push("day");

}
else {
time.push("night");

}


//Gets the user's city based on IP and replaces #homeCity span with the name of their city
// not perfectly accurate but fun


$.getJSON("http://www.telize.com/geoip?callback=?",
    function(json) {

        $("#homeCity").html(json.city);

    }
    );


//The user's username, they enter it near the beginning

var userName = []

最佳答案

我认为您在类型方面犯了错误。您将时间定义为数组并插入其中...但在另一个文件中,您将其称为字符串并对其进行类型和值比较。它永远不会等于这一行的“夜晚”,因此将始终与其他一起使用,因此默认为白天图像。

编辑:

var isDay = (hour < 20);

if (isDay) {
//do day stuff
}else{
//do night stuff
}

关于JavaScript 未显示正确的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24827228/

相关文章:

javascript - 从 javascript 访问 mvc modelstate 属性,数组属性名为 ""

javascript - 如何在 Vue 中将组件用作可重用模板?

javascript - Stream.pipe() 到 node.js 中的变量

javascript - 如何通过 jquery 使用 div id 删除 div 内的特定 html 标签及其内容?

javascript - 发生滚动后,如何使 div(菜单)固定位置?

ajax - 如何检测 ajax 调用的开始?

jquery - 在jquery中的一个循环中添加多行

javascript - Lightbox:图像描述的其他属性(而不是标题)

javascript - 尝试在 iframe 中使用 Cordova 插件

javascript - 如何从Jquery正确调用JS