Javascript JS函数不适用于IE,但适用于其他浏览器
我有一个JS函数,可以根据body标签显示不同的背景图像。它适用于除safari以外的所有浏览器。所有浏览器都基于类显示正确的图像,但是IE仅在“else”中显示图像 我不确定我错过了什么Javascript JS函数不适用于IE,但适用于其他浏览器,javascript,Javascript,我有一个JS函数,可以根据body标签显示不同的背景图像。它适用于除safari以外的所有浏览器。所有浏览器都基于类显示正确的图像,但是IE仅在“else”中显示图像 我不确定我错过了什么 <script type="text/javascript"> function loadNextImage() { if ($("body").hasClass('bodyNormal') === true) { //get image object
<script type="text/javascript">
function loadNextImage() {
if ($("body").hasClass('bodyNormal') === true) {
//get image object
var myImg = document.getElementById('homeImage');
//declare image directory path and image array
var thePath = "images/background/";
var theImages = new Array();
theImages[0] = "home1.jpg";
theImages[1] = "home2.jpg";
theImages[2] = "home3.jpg";
theImages[3] = "home4.jpg";
theImages[4] = "home5.jpg";
//get current cookie value
var currentIndex = parseInt(getCookie());
var imgPath = thePath + theImages[currentIndex];
myImg.src = imgPath;
myImg.alt = theImages[currentIndex];
myImg.title = theImages[currentIndex];
//set next cookie index
currentIndex += 1;
if(currentIndex > (theImages.length - 1)) {
currentIndex = 0;
}
setCookie(currentIndex);
}
else {
//get image object
var myImg = document.getElementById('homeImage');
//declare image directory path and image array
var thePath = "images/m-background/";
var theImages = new Array();
theImages[0] = "m-home.jpg";
theImages[1] = "m-home.jpg";
//get current cookie value
var currentIndex = parseInt(getCookie());
var imgPath = thePath + theImages[currentIndex];
myImg.src = imgPath;
myImg.alt = theImages[currentIndex];
myImg.title = theImages[currentIndex];
//set next cookie index
currentIndex += 1;
if(currentIndex > (theImages.length - 1)) {
currentIndex = 0;
}
setCookie(currentIndex);
}
}
function setCookie(someint) {
var now = new Date();
var addDays = now.getDate() + 7
now.setDate(addDays); // cookie expires in 7 days
var theString = 'imgID=' + escape(someint) + ';expires=' + now.toUTCString();
document.cookie = theString;
}
function getCookie() {
var output = "0";
if(document.cookie.length > 0) {
var temp = unescape(document.cookie);
temp = temp.split(';');
for(var i = 0; i < temp.length; i++) {
if(temp[i].indexOf('imgID') != -1) {
temp = temp[i].split('=');
output = temp.pop();
break;
}
}
}
return output;
}
</script>
函数loadNextImage(){
if($(“body”).hasClass('bodyNormal')==true){
//获取图像对象
var myImg=document.getElementById('homeImage');
//声明映像目录路径和映像数组
var thePath=“images/background/”;
var theImages=新数组();
图像[0]=“home1.jpg”;
图片[1]=“home2.jpg”;
图片[2]=“home3.jpg”;
图片[3]=“home4.jpg”;
图片[4]=“home5.jpg”;
//获取当前cookie值
var currentIndex=parseInt(getCookie());
var imgPath=路径+图像[当前索引];
myImg.src=imgPath;
myImg.alt=图像[当前索引];
myImg.title=图像[当前索引];
//设置下一个cookie索引
currentIndex+=1;
如果(currentIndex>(theImages.length-1)){
currentIndex=0;
}
setCookie(currentIndex);
}
否则{
//获取图像对象
var myImg=document.getElementById('homeImage');
//声明映像目录路径和映像数组
var thePath=“images/m-background/”;
var theImages=新数组();
图像[0]=“m-home.jpg”;
图片[1]=“m-home.jpg”;
//获取当前cookie值
var currentIndex=parseInt(getCookie());
var imgPath=路径+图像[当前索引];
myImg.src=imgPath;
myImg.alt=图像[当前索引];
myImg.title=图像[当前索引];
//设置下一个cookie索引
currentIndex+=1;
如果(currentIndex>(theImages.length-1)){
currentIndex=0;
}
setCookie(currentIndex);
}
}
函数setCookie(someint){
var now=新日期();
var addDays=now.getDate()+7
now.setDate(addDays);//cookie将在7天后过期
var theString='imgID='+escape(someint)+';expires='+now.toutString();
document.cookie=字符串;
}
函数getCookie(){
var output=“0”;
如果(document.cookie.length>0){
var temp=unescape(document.cookie);
临时=临时拆分(“;”);
对于(变量i=0;i
显然,没有设置body类,这就是上面的代码不起作用的原因。以下是设置body类的代码:
<script>
function setStyleForSize() {
if(screen.width > 480) {
document.body.className = "bodyNormal";
}
else {
document.body.className = "bodySmall";
}
}
function addEventHandler(oNode, evt, oFunc) {
if (typeof(window.event) != "undefined")
oNode.attachEvent("on"+evt, oFunc);
else
oNode.addEventListener(evt, oFunc, true);
}
addEventHandler(window, "load", function() { setStyleForSize(); } );
addEventHandler(window, "resize", function() { setStyleForSize(); } );
</script>
函数setStyleForSize(){
如果(屏幕宽度>480){
document.body.className=“bodyNormal”;
}
否则{
document.body.className=“bodySmall”;
}
}
函数加法器(oNode、evt、oFunc){
if(typeof(window.event)!=“未定义”)
oNode.attachEvent(“on”+evt,oFunc);
其他的
oNode.addEventListener(evt、oFunc、true);
}
addEventHandler(窗口,“加载”,函数(){setStyleForSize();});
addEventHandler(窗口,“调整大小”,函数(){setStyleForSize();});
控制台中是否报告了错误?如果这是设置cookie的唯一位置,则函数第一次执行时,currentIndex
可能未定义,而不是0。取决于你的getCookie函数,我猜IE调试器中$(“body”).hasClass('bodyNormal')
的值是多少?我想到的第一个问题是:body
在IE中有类吗?IE中$(“body”).hasClass('bodyNormal')
的返回值是多少?此外,您应该始终为parseInt
函数指定基数参数。我相信你想要小数点,所以parseInt('23',10)代码>