如何使用 Eclipse Link JavaScript 和 HTML
How to Link JavaScript and HTML using Eclipse
我刚刚在 codecademy 上完成了剪刀石头布练习,想尝试将它保存在我的 Eclipse 上。代码编码完美,在 codecademy 页面上运行流畅。然后我只是简单地复制所有内容并在 Eclipse 上创建 .js 工作区并将其粘贴到那里。
我的问题是,如何在 eclipse 上 run/play 它?我确实知道我也必须创建 html 工作区并且我已经这样做了,但我不确定要在 html 中放入什么。
我不是一个成熟的 programmer/web 开发者,但我希望有一天能成为一名成熟的开发者。我也从 codecademy 学到了一些关于 html 的知识。
如果有人能告诉我如何将我的 JS 代码与 html 链接起来,我将不胜感激!谢谢
JS代码:
var userChoice = prompt("Do you choose rock, paper or scissors?");
console.log("userChoice: " + userChoice);
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 {
return "paper wins";
}
} else if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else {
return "scissors wins";
}
} else if (choice1 === "scissors") {
if (choice2 === "rock") {
return "rock wins";
} else {
return "scissors wins";
}
}
};
console.log(compare(userChoice,computerChoice));
HTML代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src = "myJSCode.js"></script>
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript">
</script>
</body>
</html>
您需要确保 javascript 文件和 html 文件在同一文件夹中。
或者,如果您想将它们存储在不同的文件夹中,您可以像这样添加文件路径:
<script type="text/javascript" src = "path/myJSCode.js"></script>
我刚刚在 codecademy 上完成了剪刀石头布练习,想尝试将它保存在我的 Eclipse 上。代码编码完美,在 codecademy 页面上运行流畅。然后我只是简单地复制所有内容并在 Eclipse 上创建 .js 工作区并将其粘贴到那里。
我的问题是,如何在 eclipse 上 run/play 它?我确实知道我也必须创建 html 工作区并且我已经这样做了,但我不确定要在 html 中放入什么。
我不是一个成熟的 programmer/web 开发者,但我希望有一天能成为一名成熟的开发者。我也从 codecademy 学到了一些关于 html 的知识。
如果有人能告诉我如何将我的 JS 代码与 html 链接起来,我将不胜感激!谢谢
JS代码:
var userChoice = prompt("Do you choose rock, paper or scissors?");
console.log("userChoice: " + userChoice);
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 {
return "paper wins";
}
} else if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else {
return "scissors wins";
}
} else if (choice1 === "scissors") {
if (choice2 === "rock") {
return "rock wins";
} else {
return "scissors wins";
}
}
};
console.log(compare(userChoice,computerChoice));
HTML代码
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src = "myJSCode.js"></script>
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript">
</script>
</body>
</html>
您需要确保 javascript 文件和 html 文件在同一文件夹中。
或者,如果您想将它们存储在不同的文件夹中,您可以像这样添加文件路径:
<script type="text/javascript" src = "path/myJSCode.js"></script>