【问题标题】:How can i insert html code in a html file in expressjs?如何在 expressjs 的 html 文件中插入 html 代码?
【发布时间】:2023-02-04 03:57:17
【问题描述】:

如果没有订单,我会尝试插入 cihantoker 标签,但它不起作用。

我想将 h1 标签插入到 html 文件中,因此 res.send() 不起作用,因为它将 h1 标签插入到一个空网站上。我尝试使用 data_8+cihantoker 来实现 (data_8 是 html 文件的变量)但它不起作用。我该怎么做?

 var order_count=order_count_result[0];


//TODO:If there is no orders


                                                                                if(order_count==0){

                                                                                        data_8=data_7.replace("#no-ordered-food-div{display: none;}","no-ordered-food-div{display:inline-block;}");

                                                                                        res.write(data_8+"<h1 id='cihantoker-text'>cihantoker</h1>");

res.end();

}

              

【问题讨论】:

  • 学习 res.render 并使用模板库,然后在模板/视图中执行该逻辑
  • 顺便说一句,如果order_count_result[0]undefinedif(order_count==0){将是假的,然后猜测你的代码中的else是否有订单
  • @LawrenceCherone 不,它不是未定义的。通常 no-ordered-food-div 的显示是无;但是当我定义 order_count=0 时;我可以看到 div。

标签: javascript node.js express


【解决方案1】:

如果 + 不起作用,则 data8 是一个对象而不是字符串。

尝试:

    let h1 = "<h1 id='cihantoker-text'>cihantoker</h1>";
    let result = data8.concat(h1);
                                                                                        
    res.write(result);
    res.end();
}

查看它是否会抛出类似 concat is not a method 的错误

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    相关资源
    最近更新 更多