jQuery data()和'changeData'事件 [英] jQuery data() and 'changeData' event
本文介绍了jQuery data()和'changeData'事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以将click
事件添加到HTML元素,并让click
事件更新$(document).data(key,value)
.
I can add a click
event to an HTML element and have the click
event update $(document).data(key,value)
.
然后将触发$(document).bind('changeData', function() {...}))
.知道这一点,找出哪个HTML元素更新$(document).data()
的最佳方法是什么?
This would then trigger $(document).bind('changeData', function() {...}))
. Knowing this, what would be the best way to find out which HTML element updated $(document).data()
?
例如:
$(document).bind('changeData', function {
// get the anchor that set $(document).data()
});
$('a').click(function() {
$(document).data('key', 'value');
});
推荐答案
通过点击事件获取数据:
Well for the data by click event:
$('#elementSelector').click(function(e){
$(document).data(key, val);
});
$(document).bind('changeData', function(e){
// e.target?
});
如果没有,请尝试在存储的数据中存储目标:
If not, try storing the target within the data stored:
$('#elementSelector').click(function(e){
$(document).data(key, {
val: 'value',
target: e.target
});
});
然后在发生更改事件时:
Then when you have the change event:
$(document).bind('changeData', function(e){
var data = $(this).data(key);
var target = data.target;
var value = data.val;
});
这篇关于jQuery data()和'changeData'事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文