我正在尝试制作一个基于文本的 HTML 游戏。我来自 jQuery 的 .append 方法不起作用。当我 .append() 将段落标记添加到 a 时,它会显示一瞬间然后消失。请帮忙
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title>game</title>
<style>
</style>
</head>
<body>
<script type="text/javascript" src="jquery-3.2.1.min.js"></script>
<script type="text/javascript">
/*var input;
var room;
function submitFunction() {
alert("hello")
}
*/
</script>
<script type="text/javascript">
var input;
var room;
room = "Main Enterence"
$(document).ready(function () {
$("#mainForm").submit(function() {
input = document.getElementById("textInput").value;
if (input == "east") {
if (room == "Main Enterence") {
room = "Bedroom"
enterBedroom();
}
}
if (input == "west") {
}
if (input == "north") {
}
if (input == "south") {
}
})
})
function enterBedroom() {
$( "#consoleOutput" ).append( "<p>Test</p>" );
}
</script>
<div id="consoleOutput">
<p id="welcomeText"></p>
</div>
<form onsubmit="submitFunction()" id="mainForm">
<input type="text" id="textInput">
<input type="submit" name="sumbit">
</form>
</body>
</html>
你好, 我正在尝试制作一个基于文本的 HTML 游戏。我来自 jQuery 的 .append 方法不起作用。当我 .append() 将段落标记添加到 a 时,它会显示一瞬间然后消失。请帮忙
最佳答案
发生这种情况是因为页面已刷新 - 您提交了表单。您必须添加第一个参数以提交事件并在其上使用 preventDefault()
函数
$("#mainForm").submit(function(event) {
event.preventDefault();
});
关于javascript - 基于文本的游戏代码不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47286319/