[英]IE bug with global variables. How to bypass this bug?
As title says i have some problems with IE8 and Javascript. 如标题所述,我在IE8和Javascript方面存在一些问题。 It's known about its bug in interpretation of global variables: simply it does not get them if you don't declare as:
众所周知,它在解释全局变量时存在错误:如果不声明为:
var variable1 = something;
The problem it's i'm trying to make a script that change the body background clicking on a button and i need a global variable wrapping the actual status (what bg-x.png i'm loading). 问题是我正在尝试制作一个脚本来更改单击按钮的主体背景,并且我需要一个包装实际状态的全局变量(我正在加载bg-x.png什么)。 This script work on FF, Safari and Chrome but not, obviously, on IE.
该脚本可用于FF,Safari和Chrome,但显然不适用于IE。 Help?
救命? (the problem is on the variable "status")
(问题出在变量“状态”上)
$('#change').click(function() {
var numStates = 2;
var name = $(this).text();
if(!(status)) {
status = parseInt(1,10);
}
if(status<numStates) {
status = parseInt(status,10) + 1;
}
else {
status = parseInt(1,10);
}
alert(status);
var bgvar = null;
switch(parseInt(status,10)) {
case 1: var bgvar = ' #7097ab url(./img/bg-' + status + '.png) top center repeat';
var name = 'Pattern';
break;
case 2: var bgvar = ' #7097ab url(./img/bg-' + status + '.png) top center repeat-x';
var name = 'Sfumato';
break;
default: alert('Default');
}
$('body').css({
background:bgvar,
});
$(this).text(name);
}
);
Working code even with IE (Thanks to Zeta): 甚至使用IE都可以工作的代码(感谢Zeta):
$('#change').click(function() {
var numStates = 2;
var name = $(this).text();
// If data-status isn't defined set it to the initial value
if($('body').data('status') === undefined)
$('body').data('status',1);
// Extract the status
var status = parseInt($('body').data('status'),10);
// Handle the status
if(status < numStates)
status++;
else
status = 1;
// Save the status
$('body').data('status',status);
switch(status) {
case 1: bgvar = ' #7097ab url(./img/bg-' + status + '.png) top center repeat';
name = 'Pattern';
break;
case 2: bgvar = ' #7097ab url(./img/bg-' + status + '.png) top center repeat-x';
name = 'Sfumato';
break;
default: alert('Default');
}
$('body').css({
background:bgvar,
});
$(this).text(name);
}
);
status is a predefined member of the window-object and points to the content of the statusbar. status是窗口对象的预定义成员,并指向状态栏的内容。 Use another variable-name
使用另一个变量名
Since you're already using jQuery you could use .data()
instead of global variables: 由于您已经在使用jQuery,因此可以使用
.data()
代替全局变量:
$('#change').click(function() {
var numStates = 2;
var name = $(this).text();
// If data-status isn't defined set it to the initial value
if($('body').data('status') === undefined)
$('body').data('status',1);
// Extract the status
var status = parseInt($('body').data('status'),10);
// Handle the status
if(status < numStates)
status++;
else
status = 1;
// Save the status
$('body').data('status',status);
/* ... Rest of your code ... */
Note that this won't work in XML documents in IE (according to the jQuery doc). 请注意,这不适用于IE中的XML文档(根据jQuery文档)。
Do you know when to use »var«? 您知道何时使用»var«吗? I just cleaned your code in the follwing ways:
我只是按照以下方式清理了代码:
Delete the unnecessary comma behind "background: bgvar" which will cause errors in IE 删除“背景:bgvar”后面不必要的逗号,这将导致IE错误
$('#change').click(function () { var numStates = 2; var name = $(this).text(); var status; if (!(status)) { status = parseInt(1, 10); } if (status < numStates) { status = parseInt(status, 10) + 1; } else { status = parseInt(1, 10); } alert(status); var bgvar = null; switch (parseInt(status, 10)) { case 1: bgvar = ' #7097ab url(./img/bg-' + status + '.png) top center repeat'; name = 'Pattern'; break; case 2: bgvar = ' #7097ab url(./img/bg-' + status + '.png) top center repeat-x'; name = 'Sfumato'; break; default: alert('Default'); } $('body').css({ background: bgvar }); $(this).text(name); });
Does it work now? 现在可以用吗?
PS Use http://www.jshint.com/ to prevent those kind of errors. PS使用http://www.jshint.com/来防止此类错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.