简体   繁体   English

我如何在 ejs 中使用脚本 function?

[英]how can i use script function in ejs?

I want to use a script function in ejs.我想在 ejs 中使用脚本 function。

I thought it would be simple, but no matter how I searched, I couldn't find any information.我以为这很简单,但无论我如何搜索,我都找不到任何信息。

test.ejs测试.ejs

<body>
<h1><% sum_ab(1,2) %></h1> // I want 3 to come out here.
<script>
function sum_ab(a,b) {
 var sum = a+b;
 return sum;
}
</script>
<body>

error message: sum_ab is not defined错误信息:未定义 sum_ab

I want to know how to use script function in ejs file.我想知道如何在 ejs 文件中使用脚本 function。

Unless I'm mistaken that you cannot separate area (//this is js code) for write javascript codes.除非我弄错了,您不能分隔区域(//这是 js 代码)来编写 javascript 代码。 You can use js code within the HTML codes.您可以在 HTML 代码中使用 js 代码。 I prepared an example code following according to your code:我根据您的代码准备了一个示例代码:

            </div>
        <% function sum_ab(a,b) {
            var sum = a+b;
            return sum;
            }%>
        <h1><%= sum_ab(3,6) %></h1>
    </div>

Please try this code in the.ejs file.请在.ejs 文件中尝试此代码。 It'll be work.这将是工作。

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

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