Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将背景图像添加到EJS文件_Javascript_Express_Ejs - Fatal编程技术网

Javascript 将背景图像添加到EJS文件

Javascript 将背景图像添加到EJS文件,javascript,express,ejs,Javascript,Express,Ejs,我似乎可以将图像从app.js文件渲染到主ejs文件,但当我将其更改为背景图像时,它不会显示。知道怎么回事吗 我的app.js 从ejs页面 <tr> <% for(var i=0; i < images.length; i++){ %> <td class="vmlHero" background = "<%= images.image %>" style="background-repeat:no-repeat" bgcolor="#00

我似乎可以将图像从app.js文件渲染到主ejs文件,但当我将其更改为背景图像时,它不会显示。知道怎么回事吗

我的app.js

从ejs页面

<tr>
<% for(var i=0; i < images.length; i++){ %>

  <td class="vmlHero" background = "<%= images.image %>" style="background-repeat:no-repeat" bgcolor="#000000" width="100%" height="430" valign="top">
这:

应该是这样的:

app.get("/email", function(req, res){
  res.render("emailMain", {images: images});
});
注意额外的s

此外,此处没有使用变量i:

我认为应该是这样,或者干脆用forEach来代替

还值得注意的是,td的background属性已被弃用,您可以在样式中使用背景图像或背景来实现相同的效果。

在您的app.js中进行更改

res.render("emailMain", {image:images});
致:

并在.ejs文件中进行更改

background="<%= images.image %>"
致:


我解决了这个最简单的问题

ejs文件

将其添加到css文件中

body {
        width: 100%;
        align-items : center;
        height: 100%;
         

        /* Background image is centered vertically and horizontally at all times */
        background-position: center center;

        /* Background image doesn't tile */
        background-repeat: no-repeat;

        /* Background image is fixed in the viewport so that it doesn't move when 
        the content's height is greater than the image's height */
        background-attachment: fixed;

        /* This is what makes the background image rescale based
        on the container's size */
        background-size: cover;

        /* Set a background color that will be displayed
        while the background image is loading */
        background-color: green;
        overflow: hidden;

        /* Location of the image */
        background-image:url('images/introBackGround.jpg') ;
    }  
删除所有块引号

路由器文件

你必须加上这个

app.use(express.static(path.join(__dirname, 'public')));
文件夹

public
 └ images  
res.renderemailMain,{image:images};你是说res.renderemailMain,{images:images};
background="<%= images.image %>"
background="<%= images[i].image %>"
body {
        width: 100%;
        align-items : center;
        height: 100%;
         

        /* Background image is centered vertically and horizontally at all times */
        background-position: center center;

        /* Background image doesn't tile */
        background-repeat: no-repeat;

        /* Background image is fixed in the viewport so that it doesn't move when 
        the content's height is greater than the image's height */
        background-attachment: fixed;

        /* This is what makes the background image rescale based
        on the container's size */
        background-size: cover;

        /* Set a background color that will be displayed
        while the background image is loading */
        background-color: green;
        overflow: hidden;

        /* Location of the image */
        background-image:url('images/introBackGround.jpg') ;
    }  
app.use(express.static(path.join(__dirname, 'public')));
public
 └ images