是否有一种干净的方法可以通过点击事件之类的方式将数据从 ejs
传回 javascript 变量?我知道我可以通过一些 parentElement
、children
恶作剧从 innerHTML
或其他东西中获取数据,但它看起来有点混乱,我希望有更好的方法。我真正需要的是获取 product._id
以将其保存到购物车中,但我真的不喜欢在其中包含一个带有 <%- product._id %>
的不可见 div 以获取它的想法。>
<div class="productSection">
<% products.forEach(function(product){ %>
<div class="card">
<div class="card-title"><%- product.name %></div>
<div class="card-body">
<div class="card-body-price"><%- product.price %></div>
</div>
<button class="card-cartBtn">+</button>
</div>
<% }) %>
</div>
有什么建议吗?
答案 0 :(得分:0)
正如 Chayim Friedman 所建议的,使用 data-
属性是一个完美的解决方案。我不知道它存在。
<div class="productSection-minCard" data-id="<%= product._id %>">