javascript - jQuery.js的on方法中的data参数有什么作用?什么场景下会使用到?
本文介绍了javascript - jQuery.js的on方法中的data参数有什么作用?什么场景下会使用到?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
jQuery.js的on方法中的data参数,我是从来没有使用过,也不知道设计的时候加入这个参数的目的是什么?
这种写法:
$("div").on("click",{name:"hello"},function(e){
console.log(e.data.name);
})
不如这种写法简洁一些:
var data={name:"hello"};
$("div").on("click",data,function(e){
console.log(e.data.name);
})
那么,为什么不这样写,不是更为简洁吗?
var data={name:"hello"};
$("div").on("click",function(e){
console.log(data.name);
})
解决方案
handler
不是都是写成匿名函数的。
$(function() {
var data = { name: 'hello' };
$('div').on('click', data, onClicked);
});
function onClicked(e) {
console.log(e.data.name); // 'hello'
}
这篇关于javascript - jQuery.js的on方法中的data参数有什么作用?什么场景下会使用到?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文