【问题标题】:Calling JavaScript function from Python with js2py causes ReferenceError: document is not defined使用 js2py 从 Python 调用 JavaScript 函数会导致 ReferenceError: document is not defined
【发布时间】:2020-08-30 12:56:07
【问题描述】:

我正在尝试从 python 调用一个 javascript 函数来更改 DOM,但我无法做到。只需控制台记录一条消息即可,但是一旦我尝试影响 DOM,我就会收到标题中提到的错误。我确定这里缺少一些基本的东西...有什么建议或解决方法吗?

app.py

app = Flask(__name__)

@app.route('/')
def index():
    return render_template("index.html")

@app.route('/python_func')
def python_func():
    eval_res, jsFile = js2py.run_file('static/test.js')
    jsFile.hello()
    return "nothing"

test.js

function hello() {
    console.log("hello") <--- Works
    let div = document.createElement("div") <--- This doesn't
}

script.js(这个按钮调用python函数)

$(function() {
    $('#startbtn').bind('click', function() {
        $.getJSON('/python_func',
            function(data) {
        //do nothing
        });
        return false;
    });
});

【问题讨论】:

    标签: javascript python referenceerror


    【解决方案1】:

    我发现 Js2Py 将无法运行具有 DOM 依赖项的 JS 代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-07
      • 1970-01-01
      • 1970-01-01
      • 2016-08-30
      • 2012-03-04
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多