Javascript:检查元素是否已更改

发布于 2024-10-04 21:58:26 字数 370 浏览 6 评论 0原文

我想知道,如果可能的话,如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

遇到 2024-10-11 21:58:27

我这样做了。它运作得很好。如果我知道的话,我会使用 setProperty 方法。

function searchArray(searchValue, theArray, ChangeValue){
    for (var i=0; i < theArray.length; i++) {
        if (theArray[i].id === searchValue) {
            theArray[i].changed = ChangeValue;
        }
    }
}

function getArrayIindex(elementid, theArray){
    for (var i=0; i < theArray.length; i++) {
        if (theArray[i].id === elementid) {
            return i;
        }
    }
}

function setInputEvents(hiddenObject) {

        var element;

        for (var i = 0; i < document.forms[0].length; i++) {
            element = document.forms[0].elements[i];

            //Check to see if the element is of type input
            if (element.type in { text: 1, password: 1, textarea: 1 }) {

                arrFieldList.push({
                    id: element.id,
                    changed:false,
                    index: i,                    
                });

                element.onfocus = function () {
                    if (!arrFieldList[getArrayIindex(this.id, arrFieldList)].changed) {
                        hiddenObject.value = this.value;
                        this.value = '';
                    }
                }

                element.onblur = function () {
                    if (this.value == '') {
                        this.value = hiddenObject.value;
                    }
                }

                element.onchange = function () {
                    searchArray(this.id, arrFieldList, true);
                }
            }

        }

I did this. It works pretty well. I would have used the setProperty method if I had known.

function searchArray(searchValue, theArray, ChangeValue){
    for (var i=0; i < theArray.length; i++) {
        if (theArray[i].id === searchValue) {
            theArray[i].changed = ChangeValue;
        }
    }
}

function getArrayIindex(elementid, theArray){
    for (var i=0; i < theArray.length; i++) {
        if (theArray[i].id === elementid) {
            return i;
        }
    }
}

function setInputEvents(hiddenObject) {

        var element;

        for (var i = 0; i < document.forms[0].length; i++) {
            element = document.forms[0].elements[i];

            //Check to see if the element is of type input
            if (element.type in { text: 1, password: 1, textarea: 1 }) {

                arrFieldList.push({
                    id: element.id,
                    changed:false,
                    index: i,                    
                });

                element.onfocus = function () {
                    if (!arrFieldList[getArrayIindex(this.id, arrFieldList)].changed) {
                        hiddenObject.value = this.value;
                        this.value = '';
                    }
                }

                element.onblur = function () {
                    if (this.value == '') {
                        this.value = hiddenObject.value;
                    }
                }

                element.onchange = function () {
                    searchArray(this.id, arrFieldList, true);
                }
            }

        }
作业与我同在 2024-10-11 21:58:26

据我了解,您想要 javascript 对象属性上的 onChange 。答案是否定的,据我所知,它不存在。

但是您可以创建一个像这样的 setter 函数(作为概念证明):

var element = {};

element.setProperty = function(property, value) {
  if (typeof(element.onChange) === 'function') {
    element.onChange(property, element[property], value);
  }

  element[property] = value;
};



element.onChange = function(property, oldValue, newValue) {
  alert(property + ' changed from ' + oldValue + ' to ' + newValue);
};

element.setProperty('something', 'Hello world!');

现在您会收到一个警告框,其中显示“某些内容从未定义更改为 Hello World!”。并且 (element.something === 'Hello World!') 将返回 true

如果您现在调用:

element.setProperty('something', 'Goodbye world!');

您会收到一个警报框,其中显示“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):

var element = {};

element.setProperty = function(property, value) {
  if (typeof(element.onChange) === 'function') {
    element.onChange(property, element[property], value);
  }

  element[property] = value;
};



element.onChange = function(property, oldValue, newValue) {
  alert(property + ' changed from ' + oldValue + ' to ' + newValue);
};

element.setProperty('something', 'Hello world!');

now you get an alert box with 'something changed from undefined to Hello World!'. And (element.something === 'Hello World!') will return true.

if you now call:

element.setProperty('something', 'Goodbye world!');

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.

追星践月 2024-10-11 21:58:26

您可能会考虑突变观察者。

为此,您首先创建一个回调(当 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

黑色毁心梦 2024-10-11 21:58:26

我想您需要一种方法来捕获触发属性更改而不是属性更改的事件。属性的更改只能是由于 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.

倒数 2024-10-11 21:58:26

我相信不存在这样的事件。但是,您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文