我从 Safari 浏览器收到 Safari ReferenceError: Can't find variable: Set
错误。我已经检查了其他浏览器,但我没有收到任何此错误,所有代码对我来说都可以正常工作。
有没有人可以帮我解决这里的问题以及在所有浏览器中运行我的代码的解决方案是什么?
完整 Demo
问题行
var charactersX = new Set([
0,
32, // space
13 // enter
// add other punctuation symbols or keys
]);
// Convert characters to charCode
function toCharCodeX(char) {
return char.charCodeAt(0);
}
var forbiddenCharactersX = new Set([
toCharCodeX("_"),
toCharCodeX("-"),
toCharCodeX("?"),
toCharCodeX("*"),
toCharCodeX("\\"),
toCharCodeX("/"),
toCharCodeX("("),
toCharCodeX(")"),
toCharCodeX("="),
toCharCodeX("&"),
toCharCodeX("%"),
toCharCodeX("+"),
toCharCodeX("^"),
toCharCodeX("#"),
toCharCodeX("'"),
toCharCodeX("<"),
toCharCodeX("|"),
toCharCodeX(">"),
toCharCodeX("."),
toCharCodeX(","),
toCharCodeX(";")
]);
最佳答案
改用数组
var charactersX = [
0,
32, // space
13 // enter
// add other punctuation symbols or keys
];
并替换 .has()
,例如这里:
if (charactersX.has(code))
与
if (charactersX.indexOf(code) > -1)
和
if (forbiddenCharactersX.has(code))
还有 indexOf > -1
...
关于javascript - Safari 引用错误 : Can't find variable: Set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45312402/