簡體   English   中英

ejs文件中的“未定義文檔”錯誤,該文件將與node.js中的res.render()一起使用?

[英]“document is not defined ” error in ejs file which is to be used with res.render() in node.js?

我有這個ejs文件,該文件將在node.js中使用res.render()作為響應發送

<!DOCTYPE html>
<html>
<head>  
  <title>pilcit</title>
</head>
<body>
<% var btn = document.getElementById('btn');%>
<% btn.style.background="blue"; %>
<div class="pt-5">
<div class="container-fluid">
    <h1  style="color:blue;" align=center>Pilcit</h2>
    <h4 style="color:#369bf4" align="center">The online clipboard</h4>

     <div>

         <textarea  class="form-control mt-5" name="content" rows="12">
            <%= result.content %>
         </textarea>
         <input type="text" value="sdfdsfsdfsd"  id="foo">
         <button id="btn" data-clipboard-target="#foo" copy clip </button>


    </div>  
    <!--<input type="submit" value="Create Clip">-->

</div>

但是線
`<%var btn = document.getElementById('btn');%>

給我這個錯誤

ReferenceError: /home/ubuntu/workspace/pilcit/views/show.ejs:34
32| </head>
33| <body>
 >> 34|     <% var btn = document.getElementById('btn');%>
35| <div class="pt-5">
36| 
37|     <div class="container-fluid">

document is not defined
at eval (eval at compile (/home/ubuntu/workspace/pilcit/node_modules/ejs/lib/ejs.js:618:12), <anonymous>:17:27)
at returnedFn (/home/ubuntu/workspace/pilcit/node_modules/ejs/lib/ejs.js:653:17)
at tryHandleCache (/home/ubuntu/workspace/pilcit/node_modules/ejs/lib/ejs.js:251:36)
at View.exports.renderFile [as engine] (/home/ubuntu/workspace/pilcit/node_modules/ejs/lib/ejs.js:482:10)
at View.render (/home/ubuntu/workspace/pilcit/node_modules/express/lib/view.js:135:8)
at tryRender (/home/ubuntu/workspace/pilcit/node_modules/express

我沒有使用<%var btn = document.getElementById('btn');%>來更改btn的背景顏色,我想將其與剪貼板js一起使用。 我用那條線檢查嵌入式JavaScript是否正常工作。

您無法在ejs標記內使用document因為該代碼是在服務器上執行的。 相反,您應該添加一個腳本標記,該標記將在頁面實際加載到瀏覽器中后立即運行。

<script> var btn = document.getElementById('btn'); </script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM