gpt4 book ai didi

javascript - 如何将 JavaScript 的 console.log 输出显示到 HMTL5 页面

转载 作者:行者123 更新时间:2023-11-30 15:44:45 24 4
gpt4 key购买 nike

目标是运行 game.html,以便它调用 JavaScript 并运行 rockpapergame.js。使用 Chrome 浏览器,然后检查,在源代码中,我可以查看调试器并在脚本 src="rockpapergame.js"处跳过引用 js 的函数,然后逐步执行 rockpapergame.js。但是,我只得到以下提示:userChoice = prompt("Do you choose rock, paper or scissors?").

如何将 console.log 输出返回到 HTML5 页面?

// Rock, Paper, Scissors Game //
// rockpapergame.js //

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if(computerChoice < 0.34) {
computerChoice = "rock";
}else if (computerChoice <= 0.67) {
computerChoice = "paper";
}else{
computerChoice = "scissors";
}
console.log("Computer: " + computerChoice);

var compare = function(choice1, choice2)
{
if (choice1 === choice2) {
return "The result is a tie!";
}else if (choice1 === "rock") {
if (choice2 === "scissors") {
return "rock wins";
}else if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
}else {
return "scissors wins";
}
}else {
return "paper wins";
}
}
};

compare(userChoice, computerChoice);
// end of rockpapergame.js//
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>A Little Game</title>
</head>
<body>
<div></div>
<p><b>About this Game</b></p>
<script src="rockpapergame.js"></script>
<p>This is a cool game</p>
</body>
</html>

最佳答案

以下两种可能的解决方案。

01)正如其他人在评论中指出的那样,console.log() 始终在浏览器控制台中打印结果,而不是在页面的 HTML 中打印结果,但您可以更改其行为猴子修补(覆盖)默认 console.log 浏览器提供的函数,一个非常简单的版本可以是:

console.log = function(message) {
document.getElementById('result').innerHTML = message;
};
console.log('your result');
<div id="result"></div>

PS:我个人不建议您覆盖 console.log 默认行为。

02)您可以在 HTML 页面中添加一个包含您的结果的 div 容器,并使用该 DIV 来显示您的游戏结果。

您问题的修改版本:

// Rock, Paper, Scissors Game //
// rockpapergame.js //

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if(computerChoice < 0.34) {
computerChoice = "rock";
}else if (computerChoice <= 0.67) {
computerChoice = "paper";
}else{
computerChoice = "scissors";
}
document.getElementById('result').innerHTML = "Computer: " + computerChoice;
console.log("Computer: " + computerChoice);

var compare = function(choice1, choice2)
{
if (choice1 === choice2) {
return "The result is a tie!";
}else if (choice1 === "rock") {
if (choice2 === "scissors") {
return "rock wins";
}else if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
}else {
return "scissors wins";
}
}else {
return "paper wins";
}
}
};

compare(userChoice, computerChoice);
// end of rockpapergame.js//
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>A Little Game</title>
</head>
<body>
<div></div>
<p><b>About this Game</b></p>
<script src="rockpapergame.js"></script>
<p>This is a cool game</p>
<div id="result"></div>
</body>
</html>

关于javascript - 如何将 JavaScript 的 console.log 输出显示到 HMTL5 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40242595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com