简体   繁体   中英

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. How can I get the name (or any other property) of the clicked link for using it in my node.js?

MY HTML CODE

<!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. 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.

That said, clicks happen in the browser. Node.js, generally, is used for server-side programming. 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:

href="/goodsPage?something=shoes"

And then you can read it from the URL on the server.

If you were using Express.js then that would be a simple matter of reading the query property from the request object.

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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