为ul下的li添加侦听事件_如何将事件侦听器添加到JavaScript中的多个元素

为ul下的li添加侦听事件

In JavaScript you add an event listener to a single element using this syntax:

在JavaScript中,您可以使用以下语法将事件侦听器添加到单个元素:

document.querySelector('.my-element').addEventListener('click', event => {
  //handle click
})

But how can you attach the same event to multiple elements?

但是如何将同一事件附加到多个元素?

In other words, how to call addEventListener() on multiple elements at the same time?

换句话说,如何在多个元素上同时调用addEventListener()

You can do this in 2 ways. One is using a loop, the other is using event bubbling.

您可以通过2种方式进行操作。 一种是使用循环 ,另一种是使用事件冒泡

使用循环 (Using a loop)

The loop is the simplest one conceptually.

从概念上讲,循环是最简单的循环。

You can call querySelectorAll() on all elements with a specific class, then use forEach() to iterate on them:

您可以对具有特定类的所有元素调用querySelectorAll() ,然后使用forEach()对其进行迭代:

document.querySelectorAll('.some-class').forEach(item => {
  item.addEventListener('click', event => {
    //handle click
  })
})

If you don’t have a common class for your elements you can build an array on the fly:

如果您的元素没有通用的类,则可以动态构建一个数组:

[document.querySelector('.a-class'), document.querySelector('.another-class')].forEach(item => {
  item.addEventListener('click', event => {
    //handle click
  })
})

使用事件冒泡 (Using event bubbling)

Another option is to rely on event bubbling and attach the event listener on the body element.

另一种选择是依赖事件冒泡 ,并将事件侦听器附加到body元素上。

The event is always managed by the most specific element, so you can immediately check if that’s one of the elements that should handle the event:

该事件始终由最特定的元素管理,因此您可以立即检查该元素是否应该处理该事件:

const element1 = document.querySelector('.a-class')
const element2 = document.querySelector('.another-class')

body.addEventListener('click', event => {
  if (event.target !== element1 && event.target !== element2) {
    return
  }
  //handle click
}

翻译自: https://flaviocopes.com/how-to-add-event-listener-multiple-elements-javascript/

为ul下的li添加侦听事件

可以使用内部类来实现多个按钮添加事件侦听的功能,具体步骤如下: 1. 定义一个外部类,该类包含多个按钮对象。 2. 在外部类定义一个内部类,该内部类实现 ActionListener 接口,用于处理按钮事件。 3. 在内部类的 actionPerformed 方法,通过 getSource 方法获取事件源对象,根据事件源对象的不同,执行不同的操作。 4. 在外部类,为每个按钮对象添加事件侦听器,将内部类的实例作为参数传递给 addActionListener 方法。 示例代码如下: ``` public class ButtonDemo { private JButton button1; private JButton button2; public ButtonDemo() { button1 = new JButton("Button 1"); button2 = new JButton("Button 2"); ButtonListener listener = new ButtonListener(); button1.addActionListener(listener); button2.addActionListener(listener); } private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == button1) { // 处理 Button 1 的事件 } else if (source == button2) { // 处理 Button 2 的事件 } } } } ``` 在上面的示例代码,ButtonDemo 类包含两个按钮对象 button1 和 button2,通过内部类 ButtonListener 实现了 ActionListener 接口,用于处理按钮事件。在构造函数,为每个按钮对象添加事件侦听器,将内部类的实例作为参数传递给 addActionListener 方法。在内部类的 actionPerformed 方法,通过 getSource 方法获取事件源对象,根据事件源对象的不同,执行不同的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值