[英]jQuery - combining variable with image src to replace image attr
以下是我的脚本,该脚本旨在向用户显示基于他们当地时间的问候消息。 我从另一个堆栈溢出答案中获取了脚本,但是在替换图像src时遇到了麻烦。 我收到以下错误
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8888/neil-wp/imgurl%20+%20second.jpg
$(document).ready(function () {
datetoday = new Date(); // create new Date()
timenow = datetoday.getTime(); // grabbing the time it is now
datetoday.setTime(timenow); // setting the time now to datetoday variable
hournow = datetoday.getHours(); //the hour it is
imgselector = $('#greeting-img')
imgpath = 'wp-content/themes/neil/img/'
if (hournow >= 16.5) { // if it is after 4:30pm
imgselector.attr("src","imgpath + goodevening.gif");
}
else if (hournow >= 12) { // if it is after 12pm
imgselector.attr("src","imgpath + goodafternoon.gif");
}
else if (hournow >= 0) { // if it is after midnight
imgselector.attr("src","imgpath + goodmorning.gif");
}
});
// Wait for window load
$(window).load(function() {
$(".greeting").delay(2000).fadeOut("200");
});
这是我的HTML
<div class="greeting">
<div class="greeting-inner">
<img id="greeting-img" src="">
</div>
</div>
由于imgpath
是可变的,请使用:
imgselector.attr("src",imgpath + "goodmorning.gif");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.