Home >Web Front-end >JS Tutorial >jquery uses data method to get events on an element_jquery

jquery uses data method to get events on an element_jquery

WBOY
WBOYOriginal
2016-05-16 16:43:201241browse

JQuery’s events bound to elements can be retrieved using the data method

Get it through $(element).data("events")

// 比如给一个button绑定两个click事件

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

// 这个时候点击该button会分别弹出 2 和1的alert框
// 取出该button的所有click事件,是个数组
$("button").data('events').click

You will see two arrays of click events

Get the array, you can adjust the order and then set it back

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

The following 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>测试jquery动态改变事件</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>该demo用于测试jquery附加了事件以后可不可以动态改变事件有无,及顺序</h1> 
<input id="btn" type="button" value="点击我执行事件,分别执行三个次序弹窗"/><br/> 
<input id="btn_clear" type="button" value="点击清空执行事件,清空后第一个按钮无反应"/><br/> 
<input id="btn_revert" type="button" value="点击还原执行事件,点击后第一个按钮又会有反应"/><br/> 
<input id="btn_seq" type="button" value="点击我改变执行事件顺序,顺序会改变"/><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>
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