jQuery:如何监听DOM的变化? [英] jQuery: How to listen for DOM changes?

查看:569
本文介绍了jQuery:如何监听DOM的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可能,如何通过jQuery监听整个DOM树的更改?

Is it possible and how can I listen for changes through the entire DOM tree with jQuery?

我的具体问题:我有一个工具提示功能,显示当您在任何html元素上执行 hover 时,以标题属性的内容。然而,当您执行悬停操作时,浏览器会以标准方式在其框中呈现标题。我想表示反对。所以我想到的是将标题属性的内容移动到自定义(HTML5)数据标题属性,首次加载该页面,然后我的工具提示功能将与 data-title 一起使用。

My specific issue: I have a 'tooltip' function that displays the contents of the title attribute in a stylish way when you do a hover on any html element. When you do a hover, however, by standard the browser renders the title in its own box. I would like to supress that. So what I've thought of is to move the contents of the title attribute to a custom (HTML5) data-title attribute the first time the page is loaded, and then my tooltip function will work with data-title.

问题是后来我可能会动态添加/删除/更改HTML,所以我需要重新绑定这些元素 - 再次更改这些标题。如果有一个事件监听器会听到这样的更改并自动重新绑定元素,那将是很好的。

The problem is that later on I might add / remove / change the HTML dynamically, so I need to 'rebind' those elements - change those title attrs again. It would be nice if there was an event listener that would listen for such changes for me and rebind the elements automatically.

推荐答案

我的最好的猜测是你想听DOM突变事件。
您可以通过DOM突变事件作为任何正常的JavaScript事件(如鼠标点击)来执行此操作。

My best guess is that you want to listen to DOM mutation events. You can do that by DOM mutation event as any normal javascript event such as a mouse click.

请参考: W3 MutationEvent

示例:

$("element-root").bind("DOMSubtreeModified", "CustomHandler");

这篇关于jQuery:如何监听DOM的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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