所有浏览器的Object.watch()? [英] Object.watch() for all browsers?

查看:116
本文介绍了所有浏览器的Object.watch()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请务必阅读以下所有评论。不推荐使用Object.Watch和Object.Observe。请参阅Elliot B对更新(截至2018年6月)方法的评论。

我一直在寻找监视对象或变量以进行更改的简单方法,我找到了 Object.watch(),这在Mozilla浏览器中得到支持,但不支持IE。所以我开始四处寻找,看是否有人写过某种等价物。

I was looking for an easy way to monitor an object or variable for changes, and I found Object.watch(), that's supported in Mozilla browsers, but not IE. So I started searching around to see if anyone had written some sort of equivalent.

关于我发现的唯一一件事就是 a jQuery插件,但我不确定这是否是最佳方式。我当然在大多数项目中使用jQuery,所以我并不担心jQuery方面...

About the only thing I've found has been a jQuery plugin, but I'm not sure if that's the best way to go. I certainly use jQuery in most of my projects, so I'm not worried about the jQuery aspect...

无论如何,问题是:有人能告诉我一个有效的例子吗?那个jQuery插件?我在使它工作时遇到了问题......

Anyway, the question: Can someone show me a working example of that jQuery plugin? I'm having problems making it work...

或者,有没有人知道任何更好的替代品可以跨浏览器工作?

Or, does anyone know of any better alternatives that would work cross browser?

答案后更新

感谢大家的回复!我试用了这里发布的代码:
http:/ /webreflection.blogspot.com/2009/01/internet-explorer-object-watch.html

Thanks everyone for the responses! I tried out the code posted here: http://webreflection.blogspot.com/2009/01/internet-explorer-object-watch.html

但我似乎无法使用它IE浏览器。下面的代码在Firefox中运行良好,但在IE中没有任何作用。在Firefox中,每次更改 watcher.status 时,中的 document.write()调用watcher.watch(),您可以在页面上看到输出。在IE中,这种情况不会发生,但我可以看到 watcher.status 正在更新该值,因为最后一个 document.write() call显示正确的值(在IE和FF中)。但是,如果没有调用回调函数,那就没那么......:)

But I couldn't seem to make it work with IE. The code below works fine in Firefox, but does nothing in IE. In Firefox, each time watcher.status is changed, the document.write() in watcher.watch() is called and you can see the output on the page. In IE, that doesn't happen, but I can see that watcher.status is updating the value, because the last document.write() call shows the correct value (in both IE and FF). But, if the callback function isn't called, then that's kind of pointless... :)

我错过了什么?

var options = {'status': 'no status'},
watcher = createWatcher(options);

watcher.watch("status", function(prop, oldValue, newValue) {
  document.write("old: " + oldValue + ", new: " + newValue + "<br>");
  return newValue;
});

watcher.status = 'asdf';
watcher.status = '1234';

document.write(watcher.status + "<br>");


推荐答案

(对不起交叉发布,但这个答案我给了一个类似的问题,这里工作得很好)

(Sorry for the cross-posting, but this answer I gave to a similar question works fine here)

我创建了一个小的 object.watch shim 。它适用于IE8,Safari,Chrome,Firefox,Opera等。

I have created a small object.watch shim for this a while ago. It works in IE8, Safari, Chrome, Firefox, Opera, etc.

这篇关于所有浏览器的Object.watch()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆