Javascript &引用;“未定义文档”;要与node.js中的res.render()一起使用的ejs文件出错?

Javascript &引用;“未定义文档”;要与node.js中的res.render()一起使用的ejs文件出错?,javascript,node.js,express,Javascript,Node.js,Express,我有一个ejs文件,它将使用node.js中的res.render()作为响应发送 <!DOCTYPE html> <html> <head> <title>pilcit</title> </head> <body> <% var btn = document.getElementById('btn');%> <% btn.style.background="blue"; %> &

我有一个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>

皮尔西特
皮尔西特
在线剪贴板

但是这条线
`

给了我这个错误

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
ReferenceError:/home/ubuntu/workspace/pilcit/views/show.ejs:34
32| 
33| 
>> 34|     
35| 
36| 
37|     
未定义文档
评估时(编译时评估(/home/ubuntu/workspace/pilcit/node_modules/ejs/lib/ejs:618:12),:17:27)
在returnedFn(/home/ubuntu/workspace/pilcit/node_modules/ejs/lib/ejs.js:653:17)
在tryHandleCache(/home/ubuntu/workspace/pilcit/node_modules/ejs/lib/ejs.js:251:36)
在View.exports.renderFile[作为引擎](/home/ubuntu/workspace/pilcit/node_modules/ejs/lib/ejs.js:482:10)
在View.render(/home/ubuntu/workspace/pilcit/node_modules/express/lib/View.js:135:8)
在tryRender(/home/ubuntu/workspace/pilcit/node\u modules/express
我没有使用来更改btn的背景色,我想将其与剪贴板js一起使用。
我用这行代码检查嵌入的javascript是否工作。

您不能在ejs标记中使用
文档
,因为该代码是在服务器上执行的。相反,您应该添加一个脚本标记,该标记将在页面实际加载到浏览器中后立即运行

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

另外,该脚本必须移动到
下面,否则它将找不到它。顺序是:
在服务器上用于生成HTML->生成的文档被发送到客户端/浏览器->浏览器解析HTML并生成DOM->
文档
开始存在