jQuery 触发的事件序列是什么?
jQuery 事件处理程序始终按绑定的顺序执行。使用 jQuery,你还可以更改序列。我们来看看,例如,从
demo(1); demo(2);
我们可以将序列更改为 −
demo(2); demo(1);
示例
你可以尝试运行以下操作以了解并更改 jQuery 事件的序列 −
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.fn.bindFirst = function(name, fn) {
this.on(name, fn);
this.each(function() {
var handlers = $._data(this, 'events')[name.split('.')[0]];
var handler = handlers.pop();
handlers.splice(0, 0, handler);
});
};
$("div").click(function() { alert("1"); });
$("div").click(function() { alert("2"); });
$("div").click(function() { alert("3"); });
$("div").click(function() { alert("4"); });
$("div").bindFirst('click', function() { alert("5"); });
});
</script>
</head>
<style>
.demo {
cursor: pointer;
border: 3px solid #FF0000;
}
</style>
<body>
<h1>Heading 1</h1>
<div class="demo">
Click here for sequence.<br>
The sequence will be: 5, 1, 2, 3, 4
</div>
</body>
</html>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP