jQuery如何绑定数据绑定?

时间:2022-11-07 13:21:39
<div id="conversations-uCount">0</div>
<script type="text/javascript">
$(document).ready(function() {
    $('#conversations-uCount').data('UnreadIDs', '1');
});
</script>

How can I set a bind so that any time the UnreadIDs changes I can run a function?

如何设置一个绑定,以便在未配置文件发生更改时运行函数?

Thanks

谢谢

3 个解决方案

#1


4  

In jQuery 1.4.4+ there's an event triggered for this, changeData. If that's the only data you're dealing with on the object, your handler is as simple as:

在jQuery 1.4.4+中,会触发一个事件changeData。如果这是您在对象上处理的唯一数据,那么您的处理程序非常简单:

$('#conversations-uCount').bind("changeData", function() {
  //data changed, do something, for example:
  alert("Data changed!, new value for UnreadIDs: " + $.data(this, 'UnreadIDs'));
});

You can test it out here.

你可以在这里进行测试。

#2


1  

I imagine you could do that with a simple plugin:

我想你可以用一个简单的插件来实现:

$.fn.dataTrigger = function(name, value, callback) {
    $(this).data(name, value);
    callback(name, value);
    return this;
};

Then:

然后:

$('#conversations-uCount').dataTrigger('UnreadIDs', '1', myFunc);

Demo: http://jsfiddle.net/karim79/q6apA/1/

演示:http://jsfiddle.net/karim79/q6apA/1/

#3


0  

Yes there is a function called watch object.watch but its not standard, I think what your looking for can be found here Object.watch() for all browsers?

有一个函数叫watch object。观察,但它不是标准的,我认为您要查找的内容可以在这里找到Object.watch()用于所有浏览器?

#1


4  

In jQuery 1.4.4+ there's an event triggered for this, changeData. If that's the only data you're dealing with on the object, your handler is as simple as:

在jQuery 1.4.4+中,会触发一个事件changeData。如果这是您在对象上处理的唯一数据,那么您的处理程序非常简单:

$('#conversations-uCount').bind("changeData", function() {
  //data changed, do something, for example:
  alert("Data changed!, new value for UnreadIDs: " + $.data(this, 'UnreadIDs'));
});

You can test it out here.

你可以在这里进行测试。

#2


1  

I imagine you could do that with a simple plugin:

我想你可以用一个简单的插件来实现:

$.fn.dataTrigger = function(name, value, callback) {
    $(this).data(name, value);
    callback(name, value);
    return this;
};

Then:

然后:

$('#conversations-uCount').dataTrigger('UnreadIDs', '1', myFunc);

Demo: http://jsfiddle.net/karim79/q6apA/1/

演示:http://jsfiddle.net/karim79/q6apA/1/

#3


0  

Yes there is a function called watch object.watch but its not standard, I think what your looking for can be found here Object.watch() for all browsers?

有一个函数叫watch object。观察,但它不是标准的,我认为您要查找的内容可以在这里找到Object.watch()用于所有浏览器?