gpt4 book ai didi

javascript - 我的命令提示符 JS 代码有什么问题?

转载 作者:行者123 更新时间:2023-11-28 16:48:18 25 4
gpt4 key购买 nike

这个功能早些时候可以工作,然后就停止了。不知道为什么。它的功能应该是这样的:在与 if 语句匹配的文本框中输入命令,然后执行该命令。按 Enter 键后表单会不断刷新,所以我不认为“event.preventDefault();”最后我的 JS 代码正在运行。

HTML

<form id="command-form" autocomplete="off">
<img src="img/tint.png" alt="tint-img" id="tint-img" class="CMP">
<input type="text" placeholder="Enter Command" class="CMP" id="command-box">
<img id="x-icon" src="img/x-icon.png" alt="x-icon" class="goto-command-console bottom-left-navigation-item" onclick="toggleCMP();">
<input type="submit" style="display: none;" onclick="checkCommand();">
</form>

JavaScript

function checkCommand() {
var commandInput = document.getElementById("command-box");
var form = document.getElementById("command-form");

if ( commandInput.value === "help" ) {
window.open("html/commandhelp.html");
}
if ( commandInput.value === "windows" ) {
changeStyle('css/windows-xp.css');
}
if ( commandInput.value === "explicit pass:3XPL1C1T" ) {
changeStyle('css/explicit.css');
playMusic('other/audio/moaning.mp3');
}

form.reset();
event.preventDefault();
}

编辑

评论里一位好心的绅士解决了这个问题,去看看下面他的回答。不过,我的方法也有效。在之前的函数中,我这样做了:

function playMusic(url-to-mp3) {
new Audio(url-to-mp3).play();
}

我什至没有考虑函数对象中的破折号。两种方法都很有效,感谢您的帮助。抱歉浪费您的时间。

最佳答案

它有效:

function checkCommand(event) {
event.preventDefault();

var commandInput = document.getElementById("command-box");
var form = document.getElementById("command-form");

if ( commandInput.value === "help" ) {
window.open("html/commandhelp.html");
}
if ( commandInput.value === "windows" ) {
changeStyle('css/windows-xp.css');
}
if ( commandInput.value === "explicit pass:3XPL1C1T" ) {
changeStyle('css/explicit.css');
playMusic('other/audio/moaning.mp3');
}

form.reset();
}
<form id="command-form" onsubmit="return checkCommand(event)" autocomplete="off">
<img src="img/tint.png" alt="tint-img" id="tint-img" class="CMP">
<input type="text" placeholder="Enter Command" class="CMP" id="command-box">
<img id="x-icon" src="img/x-icon.png" alt="x-icon" class="goto-command-console bottom-left-navigation-item" onclick="toggleCMP();">
<button type="submit">Send me</button>
</form>

关于javascript - 我的命令提示符 JS 代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60267373/

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