Jquery USES the data method to get events on an element

  • 2020-03-30 03:26:54
  • OfStack

Events bound to an element in jquery can be fetched using the data method

$(element).data("events")


//For example, bind two click events to a button

$("button").click(function() { alert("1") });
$("button").click(function() { alert("2") });

//Clicking the button will bring up two and one alert boxes, respectively
//  Take out the button All of the click The event is an array 

$("button").data('events').click

You'll see an array of two click events

You get the array, you can adjust the order and set it back


$("button").data('events').click = newEventArray;

Here is the test page for this method:


<span style="font-size:18px;"><%@ page contentType="text/html;charset=UTF-8" language="java" %> 

<!DOCTYPE HTML> 
<html> 
<head> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title> test jquery Dynamically changing events </title> 

<script type="text/javascript" src="/static/lib/jquery-1.6.2.min.js"></script> 
<script type="text/javascript" src="/static/lib/jquery.cookie.min.js"></script> 
<script type="text/javascript" src="/static/lib/util.min.js"></script> 
<script type="text/javascript" src="/static/lib/jquery.bgiframe.min.js"></script> 
<script type="text/javascript" src="/static/lib/jshashtable.min.js"></script> 
</head> 
<body> 
<h1> the demo Used for testing jquery Can you dynamically change the existence and order of events after attaching them </h1> 
<input id="btn" type="button" value=" Click on the I execute event , Execute three sequence popovers separately "/><br/> 
<input id="btn_clear" type="button" value=" Click the empty execution event, the first button does not respond after the empty "/><br/> 
<input id="btn_revert" type="button" value=" Click the restore execution event, and the first button will respond "/><br/> 
<input id="btn_seq" type="button" value=" Click on me to change the order of execution, and the order will change "/><br/> 
<script type="text/javascript"> 
$(document).ready(function(){ 
$("#btn").click(function(){ 
alert(1); 
}); 
$("#btn").click(function(){ 
alert(2); 
}); 
$("#btn").click(function(){ 
alert(3); 
}); 
var _arr_events= $("#btn").data("events")["click"]; 
$("#btn_clear").click(function(){ 
$("#btn").data("events")["click"]=undefined; 
}); 
$("#btn_revert").click(function(){ 
$("#btn").data("events")["click"]=_arr_events; 
}); 

}); 
</script> 
</body> 
</html> 
</span>

Related articles: