<tfoot id='7A7rR'></tfoot>
        <bdo id='7A7rR'></bdo><ul id='7A7rR'></ul>
    1. <i id='7A7rR'><tr id='7A7rR'><dt id='7A7rR'><q id='7A7rR'><span id='7A7rR'><b id='7A7rR'><form id='7A7rR'><ins id='7A7rR'></ins><ul id='7A7rR'></ul><sub id='7A7rR'></sub></form><legend id='7A7rR'></legend><bdo id='7A7rR'><pre id='7A7rR'><center id='7A7rR'></center></pre></bdo></b><th id='7A7rR'></th></span></q></dt></tr></i><div id='7A7rR'><tfoot id='7A7rR'></tfoot><dl id='7A7rR'><fieldset id='7A7rR'></fieldset></dl></div>
      <legend id='7A7rR'><style id='7A7rR'><dir id='7A7rR'><q id='7A7rR'></q></dir></style></legend>

    2. <small id='7A7rR'></small><noframes id='7A7rR'>

    3. 在 div 更改时触发 jQuery 事件

      时间:2023-09-07

          <tbody id='g7rn6'></tbody>

        <small id='g7rn6'></small><noframes id='g7rn6'>

        1. <i id='g7rn6'><tr id='g7rn6'><dt id='g7rn6'><q id='g7rn6'><span id='g7rn6'><b id='g7rn6'><form id='g7rn6'><ins id='g7rn6'></ins><ul id='g7rn6'></ul><sub id='g7rn6'></sub></form><legend id='g7rn6'></legend><bdo id='g7rn6'><pre id='g7rn6'><center id='g7rn6'></center></pre></bdo></b><th id='g7rn6'></th></span></q></dt></tr></i><div id='g7rn6'><tfoot id='g7rn6'></tfoot><dl id='g7rn6'><fieldset id='g7rn6'></fieldset></dl></div>
            <bdo id='g7rn6'></bdo><ul id='g7rn6'></ul>
            <tfoot id='g7rn6'></tfoot>
            <legend id='g7rn6'><style id='g7rn6'><dir id='g7rn6'><q id='g7rn6'></q></dir></style></legend>

              1. 本文介绍了在 div 更改时触发 jQuery 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个 div,它的内容可能会以各种方式发生变化:例如,它的整个内容可以通过 innerHTML 重新加载,或者可以通过 DOM 方法添加节点.这反过来可能通过原生 Javascript 或通过调用 jQuery API 或通过其他库间接发生.

                I have a div whose content may change in various ways: for instance its whole content may be reloaded via innerHTML, or nodes may be added via DOM methods. This in turn may happen via native Javascript or indirectly via calls the jQuery API or via other libraries.

                我想在 div 的内容发生变化时执行一些代码,但我完全无法控制 如何改变它.事实上,我正在设计一个可供其他人使用的小部件,他们可以按照自己喜欢的方式自由更改 div 的内容.当这个 div 的内部内容发生变化时,小部件的形状可能也必须更新.

                I want to execute some code when the content of the div changes, but I have absolutely no control on how it will change. Indeed I am designing a widget that may be used by other people, who are free to change the content of their divs the way they prefer. When the inner content of this div changes, the shape of the widget may have to be updated as well.

                我正在使用 jQuery.有没有办法捕捉到这个 div 的内容发生了变化的事件?

                I'm using jQuery. Is there a way to capture the event that the content of this div has changed, however it happened?

                推荐答案

                您可以使用 DOMNodeInsertedDOMNodeRemoved 来检查是否添加或删除了元素.不幸的是,IE 不支持这个.

                You can use DOMNodeInserted and DOMNodeRemoved to check if elements are added or removed. Unfortunately, IE doesn't support this.

                $('#myDiv').bind('DOMNodeInserted DOMNodeRemoved', function(event) {
                    if (event.type == 'DOMNodeInserted') {
                        alert('Content added! Current content:' + '
                
                ' + this.innerHTML);
                    } else {
                        alert('Content removed! Current content:' + '
                
                ' + this.innerHTML);
                    }
                });
                

                <小时>

                更新

                您可以使用 .data() 保存初始内容和未来的更改.这是一个例子.


                Update

                You could save the initial contents and future changes with .data(). Here's an example.

                var div_eTypes = [],
                    div_changes = [];
                $(function() {
                    $('#myDiv').each(function() {
                        this['data-initialContents'] = this.innerHTML;
                    }).bind('DOMNodeInserted DOMNodeRemoved', function(event) {
                        div_eTypes.concat(e.type.match(/insert|remove/));
                        div_changes.concat(this.innerHTML);
                    });
                });
                

                示例输出:

                > $('#myDiv').data('initialContents');
                "<h1>Hello, world!</h1><p>This is an example.</p>"
                > div_eTypes;
                ["insert", "insert", "remove"]
                > div_changes;
                ["<iframe src='http://example.com'></iframe>", "<h4>IANA — Example domains</h4><iframe src='http://example.com'></iframe>", "<h4>IANA – Example domains</h4>"]
                

                <小时>

                更新 2

                您可能还想包含 DOMSubtreeModified,因为我发现 DOMNodeInsertedDOMNodeRemoved 不会触发元素innerHTML 被直接替换.在IE下还是不行,但至少在其他浏览器下可以正常使用.


                Update 2

                You may want to include DOMSubtreeModified as well, because I've found out that DOMNodeInserted and DOMNodeRemoved don't trigger if an element's innerHTML is replaced directly. It still doesn't work in IE, but at least it works fine in other browsers.

                这篇关于在 div 更改时触发 jQuery 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Vue html 注释处理 下一篇:可以即时进行语法突出显示的文本区域?

                相关文章

                  <legend id='M85IY'><style id='M85IY'><dir id='M85IY'><q id='M85IY'></q></dir></style></legend>
                    <bdo id='M85IY'></bdo><ul id='M85IY'></ul>
                  <tfoot id='M85IY'></tfoot>

                1. <i id='M85IY'><tr id='M85IY'><dt id='M85IY'><q id='M85IY'><span id='M85IY'><b id='M85IY'><form id='M85IY'><ins id='M85IY'></ins><ul id='M85IY'></ul><sub id='M85IY'></sub></form><legend id='M85IY'></legend><bdo id='M85IY'><pre id='M85IY'><center id='M85IY'></center></pre></bdo></b><th id='M85IY'></th></span></q></dt></tr></i><div id='M85IY'><tfoot id='M85IY'></tfoot><dl id='M85IY'><fieldset id='M85IY'></fieldset></dl></div>

                  <small id='M85IY'></small><noframes id='M85IY'>