【问题标题】:Passing link text from a hyperlink to another page. (Express JS)将链接文本从超链接传递到另一个页面。 (快递JS)
【发布时间】:2020-07-07 01:38:36
【问题描述】:

我的代码面临挑战,我想将超链接中的链接文本传递到另一个页面。我的网络应用是基于 Express 构建的。

这是一页中的超链接(在下拉列表中):

<a class="dropdown-item" href="/history" ><%=dateArray[index-1]%></a>

我要抓取链接文字 dateArray[index-1],发送到/history页面。

这是我的 app.js 中的相关代码:

app.route("/history")
.get(function(req, res) {
  getDate();
  

  List.find({}, function(err, lists) {
    res.render("history", {
      day: day,
      lists: lists
    });
  });
})

请忽略此处不相关的代码。 我应该在 app.js 中如何获取 dateArray[index-1] 值并将其发送到 /history 路由?

提前致谢。

【问题讨论】:

    标签: javascript html express


    【解决方案1】:

    您可以使用查询字符串或 express 的 req.param 来执行此操作

    请求参数(简单且更好):

    <a class="dropdown-item" href="/history/<%=dateArray[index-1]%>" ><%=dateArray[index-1]%></a>
    

    在路线中

    app.route("/history/:data")
    .get(function(req, res) {
        var urlData = req.params.data;
        console.log(urlData);
    })
    

    希望对你有帮助

    【讨论】:

    • 非常感谢 Niloy!我还在学习Web开发,肯定需要花更多的时间在文档上哈哈。再次感谢!
    • 我很高兴,总是这样。尽管继续。如果你想学习 node js 和 express,youtube 上的 net ninja 教程可能会有所帮助:youtube.com/…
    • stackoverflow.com/questions/62773679/…@Niloy 请帮帮我兄弟
    • @DharaniShankar 当然,我会尽力而为。
    • @ShiqiZhou 如果解决了你的问题,你可以标记为正确答案
    猜你喜欢
    • 1970-01-01
    • 2010-11-18
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多