所以,我对整个编码工作是个菜鸟,在完成一些培训并了解 JavaScript、Html 和 CSS 之后,我给自己设定了一个挑战。这是为了制作一台 Batcomputer/Jarvis 来管理我的谷歌议程、游戏、程序等等。但为此,我需要搜索 html 文本输入中的单词,然后启动适当的函数。
以下是重要的代码部分:
<input type="text" id="inputField" onchange="insertInput()"></input>
<script>
var currentInput = 1;
//The input and function picker
function insertInput() {
document.getElementById("inputHtml").innerHTML = currentInput;
console.log(currentInput);
var containsLaunchGame = /launch game/g;
var resultLaunchGame = currentInput.test(containsLaunchGame);
console.log(resultLaunchGame);
if (resultLaunchGame = true) {
scanGames();
console.log("I found Launch Game");
};
};
</script>
这不是全部代码,只是我有疑问的部分。 除了 string.test 之外,我还尝试了 string.search 和其他一些方法,但没有一个按照我想要的方式工作。
我想要的(并不重要如何)是我可以告诉我的电脑根据我输入的内容执行某些操作。但我尝试过的所有方法都会导致错误。
对于上面的代码,错误是“Uncaught TypeError: currentInput.test is not a function”。
如果您有任何方法可以解决此问题,无论如何,那么我将非常感激!
最佳答案
您使用“currentInput.test(containsLaunchGame);”调用的内容部分实际上是 var currentInput = 1; => 1.test()
替换这一行:
document.getElementById("inputHtml").innerHTML = currentInput;
与:
currentInput = document.getElementById("inputHtml").innerHTML;
之后替换
var resultLaunchGame = currentInput.string.indexOf(containsLaunchGame) !== -1;
console.log(resultLaunchGame);
if (resultLaunchGame = true) {
与
var resultLaunchGame = currentInput.test(containsLaunchGame);
console.log(resultLaunchGame);
if (resultLaunchGame == true) {
关于javascript - 用于选择 JavaScript 函数的 html 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40059941/