此作品在全部瀏覽器,但IE瀏覽器。我已檢查的代碼,我知道 IE瀏覽器可以正常讀取,所以你不會不知所措。 爲什麼IE說我的功能不存在?爲什麼這個JS沒有在IE中正確執行?
錯誤:
SCRIPT5009: 'switchStyles' is undefined
test.php, line 180 character 21
HTML:
025: <SCRIPT TYPE="text/javascript" SRC="/_js/styleSwitch2.js"><!-- Style Switching Scripts --></SCRIPT>
...
180: <SELECT ONCHANGE="return switchStyles(this);">
181: <OPTION VALUE="/_css/default.css">Select a style!</OPTION>
182: <OPTGROUP LABEL="Skin Group 1">
183: <OPTION VALUE="/_css/alt1-1.css">Alt 1.1</OPTION>
184: <OPTION VALUE="/_css/alt1-2.css">Alt 1.2</OPTION>
185: <OPTION VALUE="/_css/alt1-3.css">Alt 1.3</OPTION>
186: </OPTGROUP>
187: <OPTGROUP LABEL="Skin Group 1">
188: <OPTION VALUE="/_css/alt2-1.css">Alt 2.1</OPTION>
189: <OPTION VALUE="/_css/alt2-2.css">Alt 2.2</OPTION>
190: </OPTGROUP>
191: </SELECT>
JS:
var sheetElement = document.getElementById("_switchSheet");
var backupSheetElement = document.getElementById("_switchSheetBackup");
sheetElement.disabled = false;
if (backupSheetElement)
backupSheetElement.disabled = false;
else
//warn
var useCookies = true;
const COOKIE_NAME = "cookiename";
function switchStyles(selectElement)
{
if (sheetElement == null && (sheetElement = document.getElementById("_switchSheet")) == null)
//fail
switchStyleTo(selectElement.value);
}
function switchStyleTo(newSheet)
{
sheetElement.href = (newSheet == backupSheetElement.href) ? null : newSheet;
setCookie(newSheet);
}
function setUseCookies(shouldUseCookies)
{
useCookies = shouldUseCookies;
}
function setCookie(cookieContent)
{
//works
}
function getCookie()
{
//works
}
function setStyleFromCookie()
{
var cookie = getCookie();
if (cookie.length > 0)
switchStyleTo(cookie);
else
//fail
}
其IE瀏覽器您使用? – intelis 2013-03-27 00:27:49
您可能在函數存在之前調用該函數。 – Ryoku 2013-03-27 00:29:08
請作出[jsfiddle](http://jsfiddle.net/)請 – Adidi 2013-03-27 00:30:15