简体   繁体   English

如何在HTML和JS文件之间建立双向连接?

[英]How to establish two-way connection between HTML and JS files?

I'm making my first text-based interactive scenario, I want the user to enter whatever they want and click Submit.我正在制作我的第一个基于文本的交互式场景,我希望用户输入他们想要的任何内容并单击提交。 There is just title, some scenario text, a typing space and button to Submit, however when I press the submit, it always gives error that the "output" is not specified .只有标题、一些场景文本、输入空间和提交按钮,但是当我按下提交时,它总是给出"output" is not specified错误。

I've established I can use functions from HTML in JS with the "getElementById" , however this doesn't seem to work the other way around.我已经确定我可以在带有"getElementById" JS 中使用来自 HTML 的函数,但是这似乎不能反过来工作。 (Also I'm using the integrated browser emulation in Scrimba). (我也在 Scrimba 中使用集成的浏览器模拟)。 If anyone can tell me a good software they use with live representation of the code that will be huge help too.如果有人能告诉我他们使用的带有代码的实时表示的好软件,那也会有很大帮助。

I've tried moving the <script type="text/javascript" src="index.pack.js"></script> from the head, to the body, to the end of the body, and that doesn't change anything.我试过将<script type="text/javascript" src="index.pack.js"></script>从头部移动到正文,再到正文的末尾,这并没有改变任何事物。

I've also tried to change the onclick and do the entire process in JS, then just display the new text by refreshing the window, but then clicking showed no response and not even an error at all, so it was likely a wrong code.我还尝试更改onclick并在 JS 中完成整个过程,然后通过刷新窗口来显示新文本,但是单击后没有显示任何响应,甚至根本没有错误,因此它可能是错误的代码。 This is why I'd much prefer to change as little as possible about the current layout.这就是为什么我更喜欢尽可能少地更改当前布局的原因。

 var userAnswer = document.getElementById("userAnswer").value; function output(userAnswer) { var answer1 = ""; switch(userAnswer) { case 1: case "What is its name?": case "Ask for name": answer1 = document.getElementById("outputText").innerHTML = "Yes" break; } return answer1; }
 <h1>The Creature Trial</h1> <p id="outputText"><em>A creature of inconceivable dimensions encountered...</em></p> <textarea placeholder="What do you do..." id="text" rows="5" cols="25"></textarea> <input id="userAnswer" type="button" value="Proceed" onclick="output(userAnswer)"> <script type="text/javascript" src="index.pack.js"></script>

Obviously in this example I'm just trying to change the text to Yes.显然在这个例子中,我只是想将文本更改为 Yes。

Above the JS code shown here, are the 118 objects and some vars, nothing important to the problem.此处显示的 JS 代码上方是 118 个对象和一些变量,对问题没有任何重要意义。

What I want to achieve is receive the userAnswer , and from that, find certain specified keywords that link to the JS objects, and based on those keywords change the outputText "A creature of inconceivable dimensions encountered..." to the appropriate response I choose, in this case "Yes".我想要实现的是接收userAnswer ,然后从中找到链接到 JS 对象的某些指定关键字,并根据这些关键字将outputText “遇到不可思议的维度的生物...”更改为我选择的适当响应,在这种情况下“是”。

Instead, nothing happens as I click, and I receive this message in the console:相反,当我单击时没有任何反应,并且我在控制台中收到此消息:

ReferenceError: output is not defined ( https://run.scrimba.com/static/cdPLKJh8/-1/index.html:1 ) ReferenceError:未定义输出( https://run.scrimba.com/static/cdPLKJh8/-1/index.html:1

EDITS: 1. Thanks to Masoud Keshavarz for editing this post, and making its formating less of a mess.编辑: 1. 感谢 Masoud Keshavarz 编辑这篇文章,并使其格式不那么混乱。

  1. To clarify, the HTML code and the JS code are external files, they are completely separate.澄清一下,HTML 代码和 JS 代码是外部文件,它们是完全分开的。

  2. I did replace the "userAnswer" references with "text", as I was originally mistakenly referring to the button not the text that was inputted.我确实用“文本”替换了"userAnswer"引用,因为我最初错误地指的是按钮而不是输入的文本。

  3. Finally I found that after the now-Approved comment edited my code it stopped showing the error, but it truly worked when I changed the number 1 in case 1 of the switch, to a string rather than a plain number (adding quotes around it).最后,我发现在 now-Approved 评论编辑我的代码后,它停止显示错误,但是当我将开关的 case 1 中的数字 1 更改为字符串而不是普通数字(在它周围添加引号)时,它确实有效. This is still strange and I don't understand why I can only use strings not numbers but it works.这仍然很奇怪,我不明白为什么我只能使用字符串而不是数字,但它有效。

The only problem with this solution is that it doesn't solve the connection problem between the two files HTML and JS, but rather it combines them in one.这个解决方案唯一的问题是它没有解决 HTML 和 JS 两个文件之间的连接问题,而是将它们合二为一。 I would really like to have them separate or at least find out if that is possible or not?我真的很想让它们分开,或者至少看看这是否可能?

I changed a little bit your js code like this我像这样改变了你的js代码

function output() {
let userAnswer = document.getElementById("text").value;
    var answer1 = "";
    switch(userAnswer) {
     case 1:
     case "What is its name?":
     case "Ask for name":
        answer1 = document.getElementById("outputText").innerHTML = "Yes"
        break;        
}
return answer1;
}

And at your html I changed the onclick to call the function without parameter在您的 html 中,我更改了 onclick 以调用不带参数的函数

<input id="userAnswer" type="button" value="Proceed" onclick="output()">

Three issues:三个问题:

  1. You're getting the value of the wrong element.你得到了错误元素的值。 The text you're looking for is in the text element, which is a textarea, and not the userAnswer element, which is a button.您要查找的text位于text元素(一个 textarea)中,而不是userAnswer元素(一个按钮)中。

  2. You're getting the value of the element at the wrong time - when the script loads, not when the user clicks the button.您在错误的时间获取元素的值 - 脚本加载时,而不是用户单击按钮时。 When the script loads, the value of the text element is blank.当脚本加载时, text元素的值为空。 To fix this, move the var userAnswer... line inside the function.要解决此问题,请在函数内移动var userAnswer...行。

  3. You don't need to pass an output variable to the function if you're using getElementById to get the value.如果您使用getElementById获取值,则不需要将output变量传递给函数。 It doesn't hurt to leave it in, but it's not necessary.把它留在里面没有什么坏处,但没有必要。

Here's the result:结果如下:

 function output() { var userAnswer = document.getElementById("text").value; var answer1 = ""; switch(userAnswer) { case 1: case "What is its name?": case "Ask for name": answer1 = document.getElementById("outputText").innerHTML = "Yes" break; } return answer1; }
 <h1>The Creature Trial</h1> <p id="outputText"><em>A creature of inconceivable dimensions encountered...</em></p> <textarea placeholder="What do you do..." id="text" rows="5" cols="25"></textarea> <input id="userAnswer" type="button" value="Proceed" onclick="output()"> <script type="text/javascript" src="index.pack.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM