【问题标题】:How can I init brython without onload in <body>如何在 <body> 中不加载的情况下初始化 brython
【发布时间】:2017-11-27 19:48:59
【问题描述】:

似乎初始化 brython 需要这样做:

<body onload="brython()">

在链接 brython.js 后,我尝试在另一个 &lt;script&gt; 的 js 中调用 brython(),但它似乎没有被调用。

当我最终自己在浏览器控制台调用brython时,它调用了brython,甚至执行了我的python代码。

为了给出一个完整的例子来说明什么不起作用,这里有一个例子。我可能缺乏一些关于如何以及何时编译和执行 javascript 的基本知识......

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/js/brython.js"></script>
</head>

<!-- <body onload="brython()"> -->
<body>

<script type="text/javascript">
console.log("calling brython() here");
brython();
console.log("I just called brython()");

</script>
<script type="text/python">
from browser import document, alert

def echo(*args):
    alert("Hello {} !".format(document["zone"].value))

document["test"].bind("click", echo)
print("what")

</script>
<p>Your name is : <input id="zone" autocomplete="off">
<button id="test">clic !</button>
</body>

</html>

【问题讨论】:

    标签: javascript python html brython


    【解决方案1】:

    函数brython()搜索所有带有type="text/python"属性的脚本并执行里面的Python代码。

    在您的示例中,在您调用函数的那一刻,Python 脚本尚未加载,因此未找到并因此执行它也就不足为奇了。如果您将 Javascript 程序放在 Python 脚本之后,它将按您的预期工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      • 2022-12-07
      • 2020-05-21
      相关资源
      最近更新 更多