批量分配事件监听器 [英] assigning event listener in bulk

查看:96
本文介绍了批量分配事件监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将相同的eventListener分配给我流回页面的可变数量的链接。


这是代码:

I would like to assign the same eventListener to a variable number of links that I stream back to a page.

Here is the code:

展开 | 选择 | Wrap | 行号

推荐答案

原则上你必须为每个链接分配事件监听器。根据事件处理程序(以及基础HTML结构)的操作,您可以将事件分配给公共父级。


是否在捕获或冒泡模式下具有事件仅取决于处理程序的确切实现要求。

in principle you would have to assign the event listener to each of the links. depending on the action of the event handler (and the underlying HTML structure) you may assign the event to a common parent.

whether you have the event in capturing or bubbling mode solely depends on the exact implementation requirement of the handler.


在上面的代码中可以指定相同的名称。到每个链接,然后将侦听器分配给tagName?
Is is possible in the code above to assign the same "name" to each of the links and then assign the listener to the tagName instead?



不确定这是什么意思

not sure what this is supposed to mean


这里有一些简单的代码来演示我的问题。请复制并粘贴,它应该可以正常运行。


我想尝试使用你建议的编码技术。我想使用eventListeners而不是内联javascript代码。


如何将eventListener分配给下面代码中流出的每个链接,而不是onclick。事件处理程序?

Ok here is some simple code to demonstrate my question. Please copy and paste, it should run fine just as is.

I want to try to use the coding technique you have suggested. I want to use "eventListeners" instead of "inline javascript code".

How can an eventListener be assigned to each of the links streamed out in the code below instead of the "onclick" event handler?

展开 | 选择 | Wrap | 在第15行或第16行之后行号
Expand|Select|Wrap|Line Numbers


(省略onclick属性)
after line 15 or 16 (omitting the onclick attribute)
展开 | 选择 | Wrap | 行号


这篇关于批量分配事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆