gpt4 book ai didi

JavaScript 日期和浏览器版本函数

转载 作者:行者123 更新时间:2023-11-28 12:46:44 24 4
gpt4 key购买 nike

我似乎无法让这段代码工作。我试图做到这一点,如果浏览器是 IE6 或更低版本,它将使用“horarios2.png”作为 img src,而不是其他。如果是任何其他浏览器,它将检查工作日(0-6),以便填写每天的特定图像。从星期一(1)到星期三(3),图像应该是“quarta.png”。前几天有自己的形象。它们是分开工作的,但是当我尝试将它们放在一起时,我遇到了麻烦,并且它无法渲染图像。

脚本

$(document).ready (function horario () {
var date = new Date();
var weekday = (date.getDay());

function (msieversion) {
var ua = window.navigator.userAgent
var msie = ua.indexOf ( "MSIE " )

if ( msie > 0 )
return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )))
else
return 0
}
if ((msieversion() == 0 )&&(weekday==0)) {
document.getElementById('horarios').src = "img/domingo.png";}
else if ((msieversion() == 0 )&&(weekday==4)) {
document.getElementById('horarios').src = "img/quinta.png";}
else if ((msieversion() == 0 )&&(weekday==5)) {
document.getElementById('horarios').src = "img/sexta.png";}
else if ((msieversion() == 0 )&&(weekday==6)) {
document.getElementById('horarios').src = "img/sabado.png";}
else if((msieversion() <= 6 )&&(weekday>=0)) {
document.getElementById('horarios').src = "img/horarios2.png";}
else {
document.getElementById('horarios').src = "img/quarta.png";}
});

HTML

<img id="horarios" border="0" alt="" width="343" height="45" />

最佳答案

这就是我认为你的意思 DEMO HERE (mouseover the image)

function msieversion() {  
var ua = window.navigator.userAgent
var msie = ua.indexOf("MSIE ");
return (msie == -1)?0:
parseInt(ua.substring (msie+5, ua.indexOf (".", msie )))
}
function getImg(idx) {
return [
"img/domingo.png",
"img/quarta.png",
"img/quarta.png",
"img/quarta.png",
"img/quinta.png",
"img/sexta.png",
"img/sabado.png"
][idx];
}
$(document).ready(function() {
var weekday = new Date().getDay();
if (msieversion() == 0 ) $("#horarios").attr("src",getImg(weekday));
else $("#horarios").attr("src",(msieversion() <= 6 )? "img/horarios2.png":"img/quinta.png");
});

关于JavaScript 日期和浏览器版本函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6946892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com