简体   繁体   English

如何<a>在 node.js 中</a>获取点击<a>链接</a>的名称<a>?</a>

[英]How do I get the name of a clicked <a> link in node.js?

I have some links on my website.我的网站上有一些链接。 Each of the links has a name.每个链接都有一个名称。 I need to get the name of the link when I click on it to use it in node.当我单击链接以在 node.js 中使用它时,我需要获取链接的名称。 How can I get the name (or any other property) of the clicked link for using it in my node.js?如何获取在我的 node.js 中使用它的单击链接的名称(或任何其他属性)?

MY HTML CODE我的 HTML 代码

<!doctype html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Наследование в CSS</title>
  <link rel="stylesheet" type="text/css" href="page.css">

  <link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet">
</head>

<body>
  <article>
    <div>
      <ul class="nav">
        <li><a href="/goodsPage" class="link" name="shoes">Shoes</a></li>
        <li><a href="/goodsPage" class="link" name="jeans">Jeans</a></li>
        <li><a href="/goodsPage" class="link" name="t-shirts">T-shirts</a></li>
        <li><a href="/goodsPage">FAQ</a></li>
        <li><a href="/goodsPage">СТАТУС ЗАКАЗА</a></li>
    </div>
    </ul>
  </article>

The name attribute is obsolete and shouldn't be used anyway. name属性已过时,无论如何都不应该使用。 It was used to define a place to link to .它用于定义链接到的位置 The introduction of HTML 4 in 1998 replaced name on a elements with id on any element. 1998 年引入的 HTML 4 a元素上a name替换name任何元素上a id

That said, clicks happen in the browser.也就是说,点击发生在浏览器中。 Node.js, generally, is used for server-side programming. Node.js 通常用于服务器端编程。 You can't tell what user is clicking on in the browser unless you change browser-side code to pass that information to the server.除非您更改浏览器端代码以将该信息传递给服务器,否则您无法知道用户在浏览器中单击了什么。

Possibly what you should be doing is putting the data in the query string of the URL:可能您应该做的是将数据放入 URL 的查询字符串中:

href="/goodsPage?something=shoes"

And then you can read it from the URL on the server.然后你可以从服务器上的 URL 读取它。

If you were using Express.js then that would be a simple matter of reading the query property from the request object.如果您使用 Express.js,那么从请求对象中读取query属性就很简单了。

But with that pattern, it looks like putting the information in the path would be better:但是使用这种模式,看起来将信息放在路径中会更好:

href="/goodsPage/shoes"

Express.js would then let you read it from the route : Express.js 然后会让你从路由中读取它:

app.get('/goodsPage/:something', function (req, res) {
  console.log(req.params.something);
  res.send(...)
})

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

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