$.event.special

骆文彬
2023-12-01

jq 内部方法 , 很怀疑是没什么人用还是没有人用, 感觉$.trigger() 完全可以替代这个功能

 

大概就是把 bindType 和 everyName 关联起来吧? trigger 一次触发多个事件

好像还涉及到是否冒泡的问题

 

$.event.special 似乎是一个很大的内容, 以后再深入学习

 

$.event.special参考资料:
jQuery.event自定义事件机制-jQuery.event.special范例
jQuery源码学习——event.special用于执行异步任务的实践

 

 

 

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

	<head>
		<meta http-equiv="Cache-Control" CONTENT="no-cache" />
		<meta http-equiv="Pragma" CONTENT="no-cache" />
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title>$.event.special</title>
	</head>

	<body>
		<div>div</div>
	</body>

	<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
	<script type="text/javascript" th:inline="javascript">
		'use strict';

		//大概就是把 bindType 和 everyName 关联起来吧?
		$.event.special.everyName = {
			bindType: "click", //"click" : 一般事件命
			delegateType: "click",
			handle: function(e) {

				//不懂,当是语法格式吧
				//作用相当于 $(this).trigger("everyName")
				if($(e.target).is(this)) return e.handleObj.handler.apply(this, arguments)

			}
		}

		$.event.special.someName = {
			bindType: "everyName",
			delegateType: "everyName",
			handle: function(e) {
				if($(e.target).is(this)) return e.handleObj.handler.apply(this, arguments)
			}
		}

		$("div").on("someName", function() {
			console.log("someName")
		})
		$("div").on("everyName", function() {
			console.log("everyName")
		})
		$("div").on("click", function() {
			console.log("click")
		})

		$("div").trigger("click")
	</script>

</html>

 

 

 

 

 

 

相关阅读

相关文章

相关问答