1

这适用于除 IE 之外的所有浏览器。我已经审查了我知道IE 可以正确读取的代码,所以你不会不知所措。为什么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
}
4

1 回答 1

5

因为这一行是 IE 中的错误,导致其余代码无法运行。

const COOKIE_NAME = "cookiename";

更改constvar.

或者:

try {
    const COOKIE_NAME = "cookiename"; 
} catch (e) {
    var COOKIE_NAME = "cookiename";
}
于 2013-03-27T00:32:55.190 回答