【问题标题】:How to have a link that has a product id in it如何拥有一个包含产品 ID 的链接
【发布时间】:2022-01-20 13:16:08
【问题描述】:

我正在尝试在 javascript 渲染函数中创建一个链接,该链接将在我的网站上显示如下:

example.com/product/( product id number )

我的所有产品信息都在 data.js 文件中的一个数组中,该文件正在被访问,因为产品名称正确呈现购买我找不到如何制作如上所示的链接,该链接会针对每个产品进行更改(我将拥有 100 多种产品,因此我不想每次都为新产品手动更改它),但我找不到这样做的方法,我已经尝试过:

<a href='product/:_id'>
<a href='product/{product._id}'>

但这些似乎都不起作用

【问题讨论】:

    标签: javascript html arrays web


    【解决方案1】:

    我建议你使用Template literals,你是正确的,只是碰巧"

    let id;
    let a = document.querySelectorAll('a')
    a.forEach(item=>{
    item.href = `product/{id}`
    id+=1
    })
    console.log(a)
    <a ></a>
    <a ></a>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-06
      • 2020-04-22
      • 1970-01-01
      • 2018-03-23
      相关资源
      最近更新 更多