“未定义文档"ejs文件中的错误,该文件将与node.js中的res.render()一起使用? [英] "document is not defined " error in ejs file which is to be used with res.render() in node.js?

查看:30
本文介绍了“未定义文档"ejs文件中的错误,该文件将与node.js中的res.render()一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个ejs文件,该文件将使用node.js中的res.render()作为响应发送

I have this ejs file which is to be send as a response using res.render() in node.js

<!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');%>

but the line
`<% 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是否正常工作.

I'm not using the <% var btn = document.getElementById('btn');%> to change the background color of the btn, I want to use it with clipboard js. I used that line to check whether the embedded javascript is working.

推荐答案

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

You can't use document inside your ejs tags because that code is executed on the server. Instead you should add a script tag which will run as soon as the page is actually loaded in the browser.

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

这篇关于“未定义文档"ejs文件中的错误,该文件将与node.js中的res.render()一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆