如何拥有一个包含产品 ID 的 link
How to have a link that has a product id in it
我正在尝试在 javascript 渲染函数中制作一个 link,它将在我的网站上像这样出现:
example.com/product/( product id number )
我的所有产品信息都在 data.js 文件中的一个数组中如上所示,每个产品都会发生变化(我将有 100 多种产品,所以我不想每次都为新产品手动更改它),但我找不到方法来做到这一点,我已经尝试过:
<a href='product/:_id'>
<a href='product/{product._id}'>
但其中 none 似乎有效
我建议你使用 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>
我正在尝试在 javascript 渲染函数中制作一个 link,它将在我的网站上像这样出现:
example.com/product/( product id number )
我的所有产品信息都在 data.js 文件中的一个数组中如上所示,每个产品都会发生变化(我将有 100 多种产品,所以我不想每次都为新产品手动更改它),但我找不到方法来做到这一点,我已经尝试过:
<a href='product/:_id'>
<a href='product/{product._id}'>
但其中 none 似乎有效
我建议你使用 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>