检查元素是否具有事件侦听器。没有jquery [英] Check if an element has event listener on it. no jquery

查看:185
本文介绍了检查元素是否具有事件侦听器。没有jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在其上使用内联函数,如下面的代码,如何检查元素是否有事件监听器。因为我有一个函数可以调用函数并添加事件监听器,但它会导致重复事件监听器导致它触发一个函数两次。所以如何检查它,以便我可以阻止它添加一个事件监听器,如果它已经存在。谢谢! :D

how to check if an element has event listener on it, if i use an inline function on it like the code below. because i have a function that recalls the function and add the event listener but it cause to have duplication event listener causing it to trigger a function twice. so how can i check it so i can prevent it to add a event listener if is it already exist. thanks! :D

for (var a = 0;a<formFieldInput.length;a++) {
            if(formFieldInput[a].hasAttribute("name") && formFieldInput[a].attributes.title.value !== "Valid Until") {
                formFieldInput[a].addEventListener("click",function(event) {
                    toggleFieldList(event,"show");
                });
            }


推荐答案

没有JavaScript函数可以实现这一点。但是,您可以在添加侦听器时将布尔值设置为 true ,并在删除时将 false 设置为。然后在可能添加重复事件监听器之前检查此布尔值。

There is no JavaScript function to achieve this. However, you could set a boolean value to true when you add the listener, and false when you remove it. Then check against this boolean before potentially adding a duplicate event listener.

可能重复:如何检查动态连接的事件监听器是否存在?

这篇关于检查元素是否具有事件侦听器。没有jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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