我必须为我的类(class)开发一个简短的脚本,一个报价生成器,并遇到一个奇怪的问题。该脚本在 Firefox 中运行良好,但需要刷新页面才能在 Chrome 中正常运行。
我已经尝试将所有代码包含在使用 window.onload 调用的函数中,但它没有改变任何内容。 我也尝试过将 script 标签放在 html 文件的末尾,也没有任何改变,仍然是这个问题。
HTML 代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-compatible" content="IE-edge">
<meta name="viewport" content="width=device-width, initial-scale-1">
<script type="text/javascript" src="script.js"></script>
<title>Citations pour héros et héroïnes</title>
</head>
<body>
</body>
</html>
JS代码:
// Tableaux de données
const firstSFArray = ["Je suis", "Je serais", "J'ai été"];
const secondSFArray = ["un pilote", "un aventurier", "un explorateur", "un
agent", "un chercheur", "un colon"];
const thirdSFArray = ["de Vénus", "de Mars", "d'Alpha Centauri", "des tréfonds de la Galaxie", "du Néant"];
const firstFanArray = ["Je suis", "Je serais", "J'ai été"];
const secondFanArray = ["un marchand", "un guerrier", "un barbare", "un
sorcier", "un élémentaire", "un voleur"];
const thirdFanArray = ["d'Hyrule", "de Cimmérie", "des Terres du Milieu", "de Poudlard", "du Pandémonium", "de l'Autre Monde"];
// Génération de la citation
let result;
function generator(array1, array2, array3) {
const random1 = array1[Math.floor(Math.random() * array1.length)];
const random2 = array2[Math.floor(Math.random() * array2.length)];
const random3 = array3[Math.floor(Math.random() * array3.length)];
result = random1 + " " + random2 + " " + random3;
}
// Question à l'utilisateur
while (true) {
const firstQuestion = prompt("Une nouvelle citation O/N ?").toLowerCase();
if (firstQuestion === 'o') {
const secondQuestion = parseFloat(prompt('Combien de citations souhaitez-vous (1 à 5) ?', "1"));
if (secondQuestion <= 5) {
const thirdQuestion = prompt("Univers Science-Fiction (S) ou Fantastique (F) ?").toLowerCase();
if (thirdQuestion === 'science-fiction' || thirdQuestion === 's') {
for (let x = 0; x < secondQuestion; x++) {
generator(firstSFArray, secondSFArray, thirdSFArray);
const quote = result;
console.log(quote);
}
} else if (thirdQuestion === 'fantastique' || thirdQuestion === 'f') {
for (let x = 0; x < secondQuestion; x++) {
generator(firstFanArray, secondFanArray, thirdFanArray);
const quote = result;
console.log(quote);
}
}
}
} else if (firstQuestion === 'n') {
break;
}
}
获取与 Chrome 一起使用的脚本,并了解这个我不明白的问题从何而来。
最佳答案
你的脚本似乎运行得很好。在 Chrome 开发工具中测试并且有效。在没有开发工具的情况下进行了测试(正是您提供的示例),并且它有效。我的开发工具唯一提示的是“未捕获的语法错误:无效或意外的 token ”,因为在 “un
(第 2 行)之后有一个换行符,但我认为这是因为 StackOverflow 的格式?
关于javascript - 刷新前脚本在 Chrome 控制台中不可见,与 Firefox 配合良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54087390/