如何检查按钮是否具有单击侦听器

时间:2019-10-29 19:33:00

标签: javascript jquery

我想知道如何检查按钮是否具有单击侦听器。我的网页上有一个按钮,但有时单击该按钮无效。我想知道是否可以检查单击处理程序是否已附加。

我试过了,但是不行。

import regex as re
data = "Don\'s son's"
match = r"n\'s" # Should match n\'s and not n's
t = re.findall(match,data)
match1 = "n\'s" # ... Not sure what this would match
t1 = re.findall(match1,data)
match2 = "n\\'s" # Should match n\'s and not n's
t2 = re.findall(match2,data)
match3 = "n\\\'s" # ... Again not sure what this matches
t3 = re.findall(match3,data)
match4 = "n\\\\'s" # Only in here because without r prefix, some answers say you need 4 '\'s
t4 = re.findall(match4,data)
print(t)
print(match)
print(t1)
print(match1)
print(t2)
print(match2)
print(t3)
print(match3)
print(t4)
print(match4)

2 个答案:

答案 0 :(得分:2)

这取决于侦听器的应用方式。...

如果在事件属性上设置了侦听器,则可以检查该属性:

let input = document.querySelector("input");
input.onclick = function(){ console.log("I was clicked"); }

if(input.onclick){
  console.log("There is a click event listener.");
} else {
  console.log("There is NO click event listener.");
}
<input>

但是,如果侦听器是使用.addEventListener()的现代DOM API注册的,那么没有,就无法知道。

答案 1 :(得分:0)

这样的事情怎么办?

var hasOnClick = ($('#changeStatus').attr('onClick') !== undefined)