gpt4 book ai didi

javascript - If Else 用于按下按钮时的 javascript

转载 作者:太空宇宙 更新时间:2023-11-04 08:50:25 25 4
gpt4 key购买 nike

谢谢。我以前曾像你们中的许多人建议的那样做过,但我遇到的问题是我的元素是一个基于文本的游戏,根据我的选择(选项 1 或选项 2)进行更改,我尝试使用 If 语句以便代码将从一个选项运行到下一个选项,If 语句在彼此中直到结束,每个选项都包含在您按下的选项中以触发它。也许这不是解决问题的方法。

<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="dungeonStyle.css">
</head>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function codeAddress() {
document.getElementById("gameText").style.visibility = "hidden";
document.getElementById("optionOne").style.visibility = "hidden";
document.getElementById("optionTwo").style.visibility = "hidden";
document.getElementById("bTEXT").style.visibility = "hidden";
document.getElementById("bTEXT2").style.visibility = "hidden";
}
window.onload = codeAddress;
</script>
</head>

<body>

<div id="startup">
<div class="header">
<h1>DUNGEON DUELER</h1>
</div>

<p id="subtitle">The Interactive Novel</p>
<p id="gameDescription">Your choices forge your destiny</p>


<button id = "startButton" onclick="startGame()">Click to begin!
</button>

<p id="gameText">
You're in search of gold and glory. Conquer this dungeon and it shall be yours!
</p>

<button id = "optionOne" onclick="One()">Option One</button>
<button id = "optionTwo" onclick="Two()">Option Two</button>

<p2 id = "bTEXT">Begin </p2>
<p3 id = "bTEXT2">stuff</p3>

<Script>
function startGame() {
document.getElementById("startButton").style.visibility = "hidden";
document.getElementById("subtitle").style.visibility = "hidden";
document.getElementById("gameDescription").style.visibility = "hidden";
document.getElementById("gameText").style.visibility = "visible";
document.getElementById("optionOne").style.visibility = "visible";
document.getElementById("optionTwo").style.visibility = "visible";
document.getElementById("bTEXT").style.visibility = "visible";
document.getElementById("bTEXT2").style.visibility = "visible";

}
</Script>

问题

if (function one.onclick){
var str = document.getElementById("gameText").innerHTML;
var txt = str.replace("You're in search of gold and glory. Conquer this dungeon and it shall be yours!","blah blah blah");
document.getElementById("gameText").innerHTML = txt;
}
</script>

</div>
</body>
</html>

css 在单独的文件中。

我会尽力解释自己。我正在制作一个基于文本的游戏,它有两个决定结果的按钮。我正在尝试,如果按下选项一,则运行代码(更改显示的文本和 hp 系统),否则执行其他代码/选项。

第一次上传,如有错误请见谅。新手。

最佳答案

  1. 您需要为 onclick 事件 OneTwo 定义事件处理程序。在函数 OneTwo 中,您可以定义需要实现的逻辑。事件处理程序可以是 function Expressionfunction declaration .在您的情况下,它是一个函数声明。此外,您可以在参数中传递 MouseEvent 对象,该对象包含封装在对象中的事件信息,可以提供有关事件的有用信息。

  2. Script更改为script

function One(){
var str = document.getElementById("gameText").innerHTML;
var txt = str.replace("You're in search of gold and glory. Conquer this dungeon and it shall be yours!","blah blah blah");
document.getElementById("gameText").innerHTML = txt;

}


function Two(){
// The other logic

}
<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="dungeonStyle.css">
</head>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function codeAddress() {
document.getElementById("gameText").style.visibility = "hidden";
document.getElementById("optionOne").style.visibility = "hidden";
document.getElementById("optionTwo").style.visibility = "hidden";
document.getElementById("bTEXT").style.visibility = "hidden";
document.getElementById("bTEXT2").style.visibility = "hidden";
}
window.onload = codeAddress;
</script>
</head>

<body>

<div id="startup">
<div class="header">
<h1>DUNGEON DUELER</h1>
</div>

<p id="subtitle">The Interactive Novel</p>
<p id="gameDescription">Your choices forge your destiny</p>


<button id = "startButton" onclick="startGame()">Click to begin!
</button>

<p id="gameText">
You're in search of gold and glory. Conquer this dungeon and it shall be yours!
</p>

<button id = "optionOne" onclick="One()">Option One</button>
<button id = "optionTwo" onclick="Two()">Option Two</button>

<p2 id = "bTEXT">Begin </p2>
<p3 id = "bTEXT2">stuff</p3>

<script>
function startGame() {
document.getElementById("startButton").style.visibility = "hidden";
document.getElementById("subtitle").style.visibility = "hidden";
document.getElementById("gameDescription").style.visibility = "hidden";
document.getElementById("gameText").style.visibility = "visible";
document.getElementById("optionOne").style.visibility = "visible";
document.getElementById("optionTwo").style.visibility = "visible";
document.getElementById("bTEXT").style.visibility = "visible";
document.getElementById("bTEXT2").style.visibility = "visible";

}
</script>

关于javascript - If Else 用于按下按钮时的 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43479659/

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