Javascript:检查元素是否已更改
我想知道,如果可能的话,如何在 javascript 中检查元素或其属性是否已更改?
我的意思是像 window.onhashchange
这样的元素:
document.getElementById("element").onElementChange = function();
据我所知 onchange
是这样的,但是如果我想以这种方式知道它会起作用吗:
var element = {};
element.attribute = result;
element.attribute.onchange = function();
I want to know, if it's possible, how to check in javascript if an element has changed or an attribute of it?
I mean something like window.onhashchange
for an element something like:
document.getElementById("element").onElementChange = function();
As I know onchange
is something like this, but will it work if I want to know in this way:
var element = {};
element.attribute = result;
element.attribute.onchange = function();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我这样做了。它运作得很好。如果我知道的话,我会使用 setProperty 方法。
I did this. It works pretty well. I would have used the setProperty method if I had known.
据我了解,您想要 javascript 对象属性上的 onChange 。答案是否定的,据我所知,它不存在。
但是您可以创建一个像这样的 setter 函数(作为概念证明):
现在您会收到一个警告框,其中显示“某些内容从未定义更改为 Hello World!”。并且
(element.something === 'Hello World!')
将返回true
。如果您现在调用:
您会收到一个警报框,其中显示“Hello World 发生了一些变化!”再见世界!”。
当然,如果您想捕获此事件,您必须仅通过所有代码中的
setProperty
方法来设置属性!编辑:
在将来的某个时候,您也许可以使用Object.observe()。
编辑2:
现在还有代理。
As far as I understand you want onChange on javascript object Properties. The answer is no, it doesn't exist as far as I know.
But you can make a setter function like this (As a proof of concept):
now you get an alert box with 'something changed from undefined to Hello World!'. And
(element.something === 'Hello World!')
will returntrue
.if you now call:
you get an alert box with 'something changed from Hello World! to Goodbye World!'.
Off course you have to set the property only via the
setProperty
method in all of your code if you want to capture this event!Edit:
At some time in the future, you might be able to use Object.observe().
Edit 2:
Now there's also proxies.
您可能会考虑突变观察者。
为此,您首先创建一个回调(当 dom 元素更改时触发)
将其分配给观察者
varobserver = new MutationObserver(callback);
然后告诉观察者要观看什么
observer.observe ('
',observerOptions);
来自:
Mozilla 突变观察器页面
You might consider a mutation observer.
to do this you first create a callback (fired when the dom element changes)
assign it to an observer
var observer = new MutationObserver(callback);
Then tell the observer what to watch
observer.observe('<div></div>', observerOptions);
From:
Mozilla page on Mutation Observers
我想您需要一种方法来捕获触发属性更改而不是属性更改的事件。属性的更改只能是由于 CSS 或 javascript 造成的,两者都是用户操作的表现。
I guess you'd need a way to capture the event which triggered the change in attribute rather than the change in attribute. The change in attribute could only either be due to your CSS or your javascript, both being manifestations of the user's actions.
我相信不存在这样的事件。但是,您可以使用 setInterval 或 setTimeout 来监视元素更改并使用它做出相应的反应。
I believe there is no such event. However, you can use setInterval or setTimeout to watch for element changes and use it to react accordingly.