设置jQuery.data()会触发事件吗?
问题描述:
我想知道是否调用 $(。domElement)。data(key,newValue)
会触发我能处理的事件吗?我已经尝试绑定更改
但是在设置数据时这不会被触发。
I'm wondering if calls to $(".domElement").data("key", "newValue")
will trigger an event that I can handle? I've tried binding change
but this doesn't get triggered when data is set.
我认为这个问题可能会问类似的东西,但绑定 changeData
也不起作用 - jQuery data()和'changeData'事件。
I think this question may be asking something similar, but binding changeData
didn't work either - jQuery data() and 'changeData' event.
答
其实你只是试过附加自定义事件,但你也必须触发它:
Actually you have only tried to attach the custom event but you will also have to trigger it something like:
$('button').click(function (e) {
$('#someID').data("key", "newValue").trigger('changeData');
});
$('#someID').on('changeData', function (e) {
alert('My Custom Event - Change Data Called! for ' + this.id);
});