Home  >  Article  >  Web Front-end  >  How to check if an element is bound to an event in jquery

How to check if an element is bound to an event in jquery

coldplay.xixi
coldplay.xixiOriginal
2020-12-07 10:06:203124browse

Jquery method to check whether an element is bound to an event: 1. In previous versions, you can use the [obj.data('event');] method; 2. JQuery 1.8 version cancels the [obj.data] method. Change to the [$._data] method.

How to check if an element is bound to an event in jquery

The operating environment of this tutorial: windows7 system, jquery1.8 version, Dell G3 computer.

Jquery method to check whether an element is bound to an event:

On previous versions, you could call it like for other data:

obj.data ('events');

In jQuery 1.8, this direct access was removed, so in recent versions you must call it like this :

$._data(obj[0],"events")

Previous versions can useobj.data ('event'); JQuery1.8 version canceled the obj.data method and changed it to $._datamethod

Note:$._data(obj[0 ], "event") obj[0] must be added with the array[0] subscript, otherwise the data will not be obtained

-------The following is an example

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="/jquery-easyui-1.3.2/jquery-1.8.0.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#btnTest").click(function () { alert(&#39;aa&#39;); });
$("#btn").click(function () {
 
 
//判断是否绑定了click事件
var objEvt = $._data($("#btnTest")[0], "events");
if (objEvt && objEvt["click"]) {
//console.info(objEvt["click"]);
alert("bind click");
}
else {
alert("Not bind click");
}
});
 
});
</script>
</head>
<body>
<input type="button" id="btn" value="测试是否绑定事件" />
<input type="button" id="btnTest" value="被测试按钮" />
</body>
</html>

Related learning recommendations: javascript video tutorial

The above is the detailed content of How to check if an element is bound to an event in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn