html 我如何在我的脚本中定义窗花以生成随机句子?

3okqufwl  于 2023-04-18  发布在  其他
关注(0)|答案(1)|浏览(123)

在努力从存储在我的网站托管的服务器中的独立花格库中获取数据后,我决定使用以下代码的另一种方法,但它不成功,因为我无法生成所需的结果,每次加载页面时都是随机的句子:

<html>
<head>

<script defer src="js/tracery/tracery.js"></script>

<script defer src="js/tracery/tracery.min.js"></script>

<script defer src="js/tracery/jquery-1.11.2.min.js"></script>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>

<script>

var data = {

  "origin": "#salutation#, #name#",

  "salutation": ["Hi", "Good morning", "Good evening", "Good night"],

  "name": ["John", "Mary", "Peter", "Anne"]

};

var grammar = tracery.createGrammar(data);

var sentence = grammar.flatten("#origin#");

document.getElementById("output").innerHTML = sentence;

</script>
<body>
<div id="output"></div>
</body>
</html>

我在浏览器的控制台上得到以下错误:“message 7 Uncaught ReferenceError:在agronobot.html:17:15中没有定义窗格。
我调用了所有这些文件,但我认为我唯一需要的是tracery.min.js,控制台显示tracery complete,但正如我之前所说,当我加载页面时,我看不到预期的结果。

fdx2calv

fdx2calv1#

要修复这个错误,只需将<script>库放在<head>标记中,并确保首先导入jquery库。

EDIT:您可以尝试导入tracery.js的在线版本,而不是导入本地tracery.js

https://tracery.io/editor/js/tracery/tracery.js

<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
    <script src="https://tracery.io/editor/js/tracery/tracery.js"></script>
</head>

<body>
    <div id="output"></div>
     <script>
        var data = {
            "origin": "#salutation#, #name#",
            "salutation": ["Hi", "Good morning", "Good evening", "Good night"],
            "name": ["John", "Mary", "Peter", "Anne"]
        };
        var grammar = tracery.createGrammar(data);
        var sentence = grammar.flatten("#origin#");
        document.getElementById("output").innerHTML = sentence;
    </script>
</body>

</html>

相关问题