简体   繁体   English

如何在 EJS 页面上使用客户端功能

[英]How to use client side functionality on EJS page

After spending a few hours, I could not figure out the best practise to use the 'client-side' functionality on EJS page.花了几个小时后,我想不出在 EJS 页面上使用“客户端”功能的最佳实践。 The scenario is stated below:场景如下:

  1. I am using express (NodeJS) and using the EJS to render the Landing page (along with the other functionality).我正在使用 express (NodeJS) 并使用 EJS 来呈现登录页面(以及其他功能)。 Initially, I was struggling to use JQuery but I manage to require it to the 'app.js' (definitely, not the best practice but could not find alternative solution hence this question).最初,我在努力使用 JQuery 但我设法将它要求用于“app.js”(当然,这不是最佳实践,但找不到替代解决方案,因此这个问题)。 So, coming to the original question.所以,来到最初的问题。 I want to use some the client-side scripting on the EJS page but as that page has server-side code not entirely sure how to use client side scripting on it (JQuery/Javascript, i,e. on scroll do that etc.).我想在 EJS 页面上使用一些客户端脚本,但由于该页面具有服务器端代码,因此不完全确定如何在其上使用客户端脚本(JQuery/Javascript,即在滚动时执行此操作等) .
  2. My header.ejs file contains some menu items which I want to be dynamic, ie top menu bar to be sticky but as that navbar is in the header.ejs file not sure how can I do that from client-side.我的 header.ejs 文件包含一些我想要动态的菜单项,即顶部菜单栏是粘性的,但由于导航栏位于 header.ejs 文件中,因此我不确定如何从客户端执行此操作。 Currently, I could not call that do that from the one EJS file to another EJS, see below.目前,我不能从一个 EJS 文件到另一个 EJS 调用它,见下文。
    // Sticky bar
    window.onscroll = function() {myFunction()};

    var navbar = document.getElementById("navbar");
    var sticky = navbar.offsetTop;

    function myFunction() {
      if (window.pageYOffset >= sticky) {
        navbar.classList.add("sticky")
      } else {
        navbar.classList.remove("sticky");
      }
    }

When you use js files in ejs, it is like you don't use ejs or nodejs.当你在 ejs 中使用 js 文件时,就像你不使用 ejs 或 nodejs。 It's just static files.这只是 static 文件。 And you can't use npm package.而且你不能使用 npm package。

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

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