简体   繁体   English

DOMSubtreeModified不会在$ .hide()和$ .show()上触发

[英]DOMSubtreeModified doesn't fire on $.hide() and $.show()

jsfiddle example link jsfiddle示例链接

Seems like jQuery $.hide() and $.show() do not modify the DOM to hide/show the element, and hence do not fire DOMSubtreeModified event. 似乎jQuery $.hide() $.show() $.hide()$.show()不会修改DOM来隐藏/显示元素,因此不会触发DOMSubtreeModified事件。 How can I capture those kind of events on any element on the DOM? 如何在DOM的任何元素上捕获此类事件?

Thanks. 谢谢。

UPDATE: The code doesn't work on Chrome16 Ubuntu. 更新:该代码在Chrome16 Ubuntu上不起作用。 Originally, I need to target QT browser, which is based on Webkit. 最初,我需要针对基于Webkit的QT浏览器。

Your example JSFiddle works for me in Chrome 17. I believe your problem is a browser support one. 您的示例JSFiddle在Chrome 17中适用于我。我相信您的问题是浏览器支持的问题。 I'm not sure of the exact browser support but not all browsers support the DOMSubtreeModified event. 我不确定确切的浏览器支持,但并非所有浏览器都支持DOMSubtreeModified事件。 I do know that DOMSubtreeModified is not supported in IE8. 我知道IE8不支持DOMSubtreeModified

And .hide() / .show() alter the elements style attribute, adding display:none / display:(block|list-item|etc.) 然后.show() .hide() / .show()更改元素的style属性,添加display:none / display:(block|list-item|etc.) .show() display:(block|list-item|etc.)

Update 更新

I recommend triggering a custom event on element you alter, then listening for that event: 我建议在您更改的元素上触发自定义事件,然后监听该事件:

//register event handler
$(document).on('customDOMAltered', function (event) {
    console.log(event.target);
});

//alter DOM element
$('#some-element').hide().trigger('customDOMAltered');

you are correct, DOMSubtreeModified is purely for modifications so you have a few choices: 您是正确的, DOMSubtreeModified仅用于修改,因此您有几种选择:

1) use DOMAttrModified which has just as bad if not worse support than DOMSubtreeModified 1)使用DOMAttrModified ,它的支持程度DOMSubtreeModifiedDOMSubtreeModified

2) override the .hide() and .show() methods in jQuery so you can capture any calls to them and respond: 2)在jQuery中覆盖.show() .hide().show()方法,以便您可以捕获对它们的任何调用并进行响应:

var oldShow = $.fn.show;
$.fn.show = function(args) {
    alert('showing!');
    oldShow.apply(this, arguments);
};
// do the same for .hide()

3) run some kind of "monitor" in a loop (eg. setInterval() ), looking at the visible children counts or something to that affect 3)在循环中运行某种“监视器”(例如setInterval() ),查看可见的子计数或有影响的东西

4) re-organize the way you are thinking about the problem... 4)重新组织您思考问题的方式...

hope this helps -ck 希望这对您有帮助-ck

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM