最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make an modal with ejs and node - Stack Overflow

programmeradmin6浏览0评论

I'm trying to learn a bit of ejs and node, I know that I can easily create a pop-up modal with only JS and HTML like this calling a function when I click certain button

Button to call the function:

<button id="myBtn">Open Modal</button>

Function:

btn.onclick = function() {
   modal.style.display = "block";
}

But how can I do this with Ejs and NodeJs?

I'm trying to learn a bit of ejs and node, I know that I can easily create a pop-up modal with only JS and HTML like this calling a function when I click certain button

Button to call the function:

<button id="myBtn">Open Modal</button>

Function:

btn.onclick = function() {
   modal.style.display = "block";
}

But how can I do this with Ejs and NodeJs?

Share Improve this question asked Jan 2, 2020 at 14:05 KAK4SHI HATAK3KAK4SHI HATAK3 1171 silver badge9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

It doesn't make sense to do this with EJS and Node. In this context those are tools for running server-side code. It makes more sense to use the existing JS you have as client-side JavaScript (in the HTML document you are generating from the EJS).

That said, if you really wanted to do it server-side, then you'd want something like this in the HTML:

<form method="post">
    <button name="modal" value="open">Open Modal</button>
</form>

And then (assuming you are using Express JS) something like:

const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded());
app.post("/your/page", function (req, res) {
    const show_modal = !!req.body.modal; // Cast to boolean
    res.render("page", { show_modal });
}

And then in the EJS:

<% if (show_modal) { %>
    <div id="modal" style="display: block">
        etc
    </div>
<% } %>
发布评论

评论列表(0)

  1. 暂无评论