【发布时间】:2021-11-30 23:10:52
【问题描述】:
我正在编写一个简单的应用程序,以将项目添加到购物袋中以研究 Javascript。但是,单击该元素时我的 js 代码没有注册,它没有在控制台上打印。
window.onload = function() {
//your code here
const fruitList = [{
'fruit': 'Banana',
'price': 3.9
},
{
'fruit': 'Orange',
'price': 0.7
},
]
const product = document.getElementById('products')
ListaFrutas.map((n) => {
product.insertAdjacentHTML('afterbegin', '<li class="fruits">' + n.fruit + '</li>');
})
const shoppingBag = []
const fruitproduct = document.querySelector('.fruits')
fruitproduct.addEventListener("onClick", handlerClick)
function handlerClick() {
console.log('it works')
}
}
<div id="content-produtos" class="flex" >
<ul id="produtos" >
</ul>
</div>
【问题讨论】:
-
显示的代码甚至不会到达
addEventListener,因为它在尝试使用未定义的ListaFrutas时失败。 -
您还通过 ID
products检索元素,但您的 HTML 的 ID 为produtos
标签: javascript html