简体   繁体   English

Node.js - 在app.js中获取href的ID

[英]Node.js - Get ID of href in app.js

I am using an href to link the user to a route. 我正在使用href将用户链接到路由。 My problem is that I need to send a parameter to the dashboard while clicking on the link. 我的问题是我需要在点击链接时向仪表板发送参数。 The easiest way to do this would be to get the id or name of the href in the app.js and send it to the dashboard. 最简单的方法是获取app.js中href的id或名称,并将其发送到仪表板。

Here is my code: 这是我的代码:

 //the href in the index.ejs
 <a href="/dashboard" id="ANDROID">ANDROID</a> 

 //in the app.js
 app.get('/dashboard', function(req, res) {
     var url = req.url;
     console.log("url: " + url);    //prints out url: /dashboard
     var id = req.url.id;
     console.log("id: " + id);      //prints out id: undefined
 });

How can I grap the id or name of the href? 如何绘制href的id或名称?

If I understand correctly, you need to pass the parameter to the URL. 如果我理解正确,您需要将参数传递给URL。 The ID of the "a" element is only a DOM ID and it is not in any way connected to the URL the link leads to. “a”元素的ID只是一个DOM ID,它不以任何方式连接到链接所指向的URL。

You could try something like this: 你可以尝试这样的事情:

<a href="/dashboard?id=ANDROID" id="ANDROID">ANDROID</a>

Then in the code: 然后在代码中:

app.get('/dashboard', function(req, res) {
    var id = req.query.id;
    ...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM