如何将textarea的内容放到一个单独的JavaScript文件中?

ffvjumwh  于 12个月前  发布在  Java
关注(0)|答案(1)|浏览(92)

有没有可能在不将JavaScript文件插入HTML代码的情况下,将textarea元素的内容获取到单独的JavaScript文件中?我无法将JavaScript文件插入到HTML文件中,因为MathSteps在浏览器中无法工作。
下面是JavaSript文件:

question = getTextareaContentsById("question")

const mathsteps = require('mathsteps');
const steps = mathsteps.solveEquation(question);

steps.forEach(step => {
    console.log("before change:");
    console.log(step.oldEquation.ascii());
    console.log("change:");
    console.log(step.changeType);
    console.log("after change:");
    console.log(step.newEquation.ascii());
    console.log("# of substeps:");
    console.log(step.substeps.length);
});

下面是HTML文件:

<!DOCTYPE html>
<html>
<head>
    <title>Step By Step Calculator</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto">
    <style>
        body {
            font-family: Roboto, sans-serif;
            margin: 10px;
            padding: 0;
            background-color: white;
            text-align: center;
        }
        .container {
            display: block;
        }
        #question {
            margin: 0 auto;
        }
        #submit-button {
            width: 10vw;
            margin: 0 auto;
            align-items: center;
            text-align: center;
            display: block;
        }
        #steps {
            text-align: center;
            margin-left: 50%;
            transform: translateX(-50%);
            background-color: aquamarine;
            width: 200px;
        }
        #steps:nth-child(even) {
            background-color: antiquewhite;
        }
    </style>
</head>
<body>
<div class="container">
    <h2 id='header'>Enter a math problem:</h2>
    <textarea id="question" cols="90" rows="1"></textarea>
    <p>Steps:</p>
    <div id="steps-container">
        {% for line in stdout_lines %}
        <p class="steps">{{ line | safe }}</p>
        {% endfor %}
    </div>
</div>
</script>
</body>
</html>

下面是Python文件:'''

# main.py
from flask import Flask, render_template
import subprocess

app = Flask(__name__)

@app.route("/")
def home():
    result = subprocess.run(["/usr/bin/node", "mathsteps.js"], capture_output=True, text=True)
    stdout = result.stdout
    # Erota tulosteet rivinvaihdolla
    stdout_lines = stdout.split("\n")

    return render_template("home.html", stdout_lines=stdout_lines)

if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0', port='3002')

'''
我对JS、CSS和HTML都很陌生,所以请原谅我。谢谢你,谢谢

9q78igpj

9q78igpj1#

您需要关注的关键点是:

  • 您有一个HTTP服务器(用Python编写),Web浏览器可以从其中请求HTML文档。
  • 我假设您希望浏览器的用户在该文本区域中输入一些数据,并将其发送到服务器进行处理
  • 您希望使用JavaScript库处理它

从浏览器获取数据到服务器

introductory forms tutorial开始,它覆盖了sending form data

处理提交的数据

  • 为URL创建路由
  • 读取表单数据

传递给Node程序

您的Python程序似乎没有任何理由使用Python。您的项目似乎围绕着Node.js库。考虑使用Node.js和express library重写HTTP服务器。这将比运行一个外部进程并将数据传递给它更容易。

如果你坚持使用Python:

相关问题