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

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

问题描述

请注意Object.WatchObject.Observe 现在都已弃用(截至 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.

关于我发现的唯一东西是一个 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 中不起作用.在火狐浏览器中,每次watcher.status改变时,都会调用watcher.watch()中的document.write(),可以看到页面上的输出.在 IE 中,这不会发生,但我可以看到 watcher.status 正在更新值,因为最后一个 document.write() 调用显示了正确的值(在 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天全站免登陆