为什么下拉菜单在节点Project的ejs文件中不起作用?

时间:2019-03-04 16:36:39

标签: javascript html node.js ejs

我已经在nodejs项目的.ejs文件中同时包含了javascript和bootstrap。引导CSS可以在按钮上运行(例如btn btn-primary可以工作),但是下拉菜单不起作用。
//我的index.ejs文件

<!DOCTYPE html>
<html>
    <head>
        <title>Home automation system</title>
        <script src="js/bootstrap.min.js"></script>
        <script src="js/myscript.js"></script>
        <link rel="stylesheet" href="/css/bootstrap.min.css">
        <link rel="stylesheet" href="/css/mycss.css">

    </head>
    <body>
        <div class="container">
            <div class="test text-center" style="margin-bottom:0">
                <h1>Home Automation system</h1>
                <p>The first Home Automation system in Bhutan</p> 
            </div>
            <% include partials/navbar.ejs %>
            <br><br>
            <div class="dropdown">
                    <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                      Dropdown button
                    </button>
                    <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                      <a class="dropdown-item" href="#">Action</a>
                      <a class="dropdown-item" href="#">Another action</a>
                      <a class="dropdown-item" href="#">Something else here</a>
                    </div>
                  </div>
        </div>
    </body>
</html>  

// app.js

var express = require('express');
var lightController = require('./controllers/lightController');

var app=express();

//set the template engine
app.set('view engine', 'ejs');

//link static files
app.use(express.static('./public'));

//to fire the controller
lightController(app);

//listen on port
app.listen(3000);
console.log('server running ...');

我已经包含了从bootstrap 4.3(下载)中提取的js和css文件夹,并包含在公用文件夹中。

1 个答案:

答案 0 :(得分:0)

首先,请执行以下操作:

<%- include('partials/navbar'); -%>

您需要jQuery使用data-toggle,脚本始终会显示在页面底部。

<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="js/bootstrap.min.js"></script> <!-- make sure that the jquery script is always first -->
<script src="js/myscript.js"></script>
</body>
</html>