Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
762 views
in Technique[技术] by (71.8m points)

html - How to use jQuery (innerHTML) in an ejs template?

I'm trying to pass a dynamic value(ejs) from one page to another. I'm trying to get the value into a variable and the console logging it but nothing happens. Here is my code

<%- include('partials/headerNavbar') %>



<%  allMovies.forEach(function(item){ %>
<div class="movie_card">

  <div class="main_box">
    <!-- var linkToImage = <%=item.wallpaper%> -->
    <div class="img_box" style="background-image:url(<%=item.wallpaper%> )">

    </div>
    <div class="content_box">
      <h1> <%=item.title%> </h1>
      <h2> <%=item.director%> </h2>
      <h3> <%=item.genre%> </h3>
      <p> <%=item.desc%> </p>
      <button type="button" id="specReview" onclick="myFunction(); location.href='/specificReview';" class="btn btn-outline-warning">Write A Review</button>
      <button type="button" class="btn btn-outline-warning">Add To Watchlist</button>
      <button type="button" class="btn btn-outline-warning">Watch Trailer</button>

<!-- onclick="location.href='/specificReview'" -->

    </div>
  </div>
</div>

<script>
    function myFunction(){
      var name = $("h1").val();
      console.log(name);
    }
</script>


<%  }) %>

<%- include('partials/footer') %>

The script doesn't seem to work. Any help will be appreciated, thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Server side: Installing jQuery with npm in Node, and doing var $ = require('jquery') on the serverside.
  2. Client side: You need import jquery file into target head Example:

<head>
    <title>Whatever App</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> 
    </script>
</head>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...