let lis = documentquerySelectorAllli;使用原生js给通过该方法获取的所有li添加一个点击事件
To add a click event to each li element using vanilla JavaScript, you can iterate over the li elements using a forEach loop and attach an event listener to each element. Here's an example:
let lis = document.querySelectorAll('li');
lis.forEach(li => {
li.addEventListener('click', function() {
// Your click event logic here
console.log('Clicked on ' + li.textContent);
});
});
In this example, the forEach loop iterates over each li element and attaches a click event listener to it. The event listener function is defined using an anonymous function, which will be executed when the element is clicked. You can replace the console.log statement with your own click event logic
原文地址: http://www.cveoy.top/t/topic/i0JI 著作权归作者所有。请勿转载和采集!