jQuery:如何检测html是否在给定元素中发生了变化? [英] jQuery: How can I detect if the html has changed within a given element?

查看:91
本文介绍了jQuery:如何检测html是否在给定元素中发生了变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript中,可能使用jQuery,我如何检测给定元素的html内容是否已更改?

In javascript, possibly using jQuery, how can I detect if the html content of a given element has changed?

我希望能够做某事喜欢:

I'd like to be able to do somthing like:

$('#myDiv').change(function(){
  // do some stuff
});

我基本上试图检测给定元素是否被添加到div或者内部html是给定的元素(如标签)已更改,然后根据内容隐藏或显示div。

I am basically trying to detect if given elements are being added to the div or if the inner html of given elements (such as labels) has changed and then hide or show the div depending on the content.

关于如何实现这样的smt的任何其他想法也很受欢迎

我希望我不必回复到一些不起眼的插件来执行此操作!

I am hoping I won't have to revert to some obscure plugin to do this!

注意:这至少需要在IE8中运行!

NOTE: this needs to work at least in IE8!

推荐答案

您可以使用DOM Level 3事件 DOMNodeInserted 。示例:

You can use the DOM Level 3 event DOMNodeInserted. Example:

$('#myDiv').bind('DOMNodeInserted', function(e) {
    console.log('element: ', e.target, ' was inserted);
});

演示 http://www.jsfiddle.net/vsgdZ/1/

活动将每当新节点附加到绑定处理程序的元素时触发。

The event will fire whenever a new node was appended to the element on which you bind the handler.

这篇关于jQuery:如何检测html是否在给定元素中发生了变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆