【问题标题】:Require Id of html tag in app.js from ejs file需要 ejs 文件中 app.js 中 html 标记的 ID
【发布时间】:2020-08-28 04:29:50
【问题描述】:

sort.ejs 文件代码

<html>
<form action="/" method="post">
        <button type="submit" name="bubble" value="bubble">Bubble Sort</button>
</form>

<hr id = "123" style="height:<%=hgt%>px;border-left: <%=width%>px solid black;float: left;margin-left:<%=margin%>px">

</html>

app.js 文件代码

app.get("/", function(req, res) {
    res.render("sort",{array:array,size:200,flag1:0,flag2:0,flag3:0,flag4:0});
});

app.post("/",function(req,res){


}

当发布请求被触发时,我想在终端的 sort.ejs 文件中打印 hr 标签的样式元素(高度、边距)的值。 sort.ejs 中使用的所有变量都是预定义的。
我正在使用 Node.js 和 EJS。

【问题讨论】:

    标签: javascript html node.js ejs


    【解决方案1】:

    我对您的查询有点困惑。但如果您询问如何从 app.js 获取价值并在 sort.ejs 中设置,那么您应该使用 标签而不是 。

    所以你的样子如下:

    <hr id = "123" style="height:<%- hgt %>px;border-left: <%- width %>px solid black;float: left;margin-left:<%- margin %>px">
    

    这里有一些关于 ejs 中不同标签的基本信息。

    将值输出到模板中(HTML 转义)

    将未转义的值输出到模板中

    更多详情可以参考这个链接:EJS Reference

    【讨论】:

    • 我想使用 body-parser 或 app.js 文件中的任何其他工具获取 hr 标签的样式值。实际上,我在 sort.ejs 文件中有一个 for 循环,并为所有 hr 标签提供了唯一的 id,并且每个 hr 标签的所有样式值(高度,左边框)在 for 循环中也是唯一定义的,我想存储其样式的值在 app.js 文件中的数组中。
    猜你喜欢
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 2016-11-08
    相关资源
    最近更新 更多