diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/mutationobserver/index.html | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/mutationobserver/index.html')
-rw-r--r-- | files/zh-cn/web/api/mutationobserver/index.html | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/mutationobserver/index.html b/files/zh-cn/web/api/mutationobserver/index.html new file mode 100644 index 0000000000..21cd5d2ffc --- /dev/null +++ b/files/zh-cn/web/api/mutationobserver/index.html @@ -0,0 +1,110 @@ +--- +title: MutationObserver +slug: Web/API/MutationObserver +tags: + - API + - Advanced + - DOM + - DOM Reference + - MutationObserver + - requestAnimationFrame + - resize +translation_of: Web/API/MutationObserver +--- +<p>{{APIRef("DOM WHATWG")}}</p> + +<p>{{domxref("MutationObserver")}}接口提供了监视对DOM树所做更改的能力。它被设计为旧的Mutation Events功能的替代品,该功能是DOM3 Events规范的一部分。</p> + +<h2 id="构造函数">构造函数</h2> + +<dl> + <dt>{{domxref("MutationObserver.MutationObserver", "MutationObserver()")}}</dt> + <dd>创建并返回一个新的 <code>MutationObserver</code> 它会在指定的DOM发生变化时被调用。</dd> +</dl> + +<h2 id="Instance_methods" name="Instance_methods">方法</h2> + +<dl> + <dt>{{domxref("MutationObserver.disconnect", "disconnect()")}}</dt> + <dd><code><font face="Open Sans, arial, x-locale-body, sans-serif"><span style="background-color: #ffffff;">阻止 </span></font>MutationObserver</code> 实例继续接收的通知,直到再次调用其{{domxref("MutationObserver.observe", "observe()")}}方法,该观察者对象包含的回调函数都不会再被调用。</dd> + <dt>{{domxref("MutationObserver.observe", "observe()")}}</dt> + <dd>配置<code>MutationObserver</code>在DOM更改匹配给定选项时,通过其回调函数开始接收通知。</dd> + <dt>{{domxref("MutationObserver.takeRecords", "takeRecords()")}}</dt> + <dd>从MutationObserver的通知队列中删除所有待处理的通知,并将它们返回到{{domxref("MutationRecord")}}对象的新{{jsxref("Array")}}中。</dd> +</dl> + +<h2 id="Mutation_Observer_customize_resize_event_listener_demo">Mutation Observer & customize resize event listener & demo</h2> + +<p><a href="https://codepen.io/webgeeker/full/YjrZgg/">https://codepen.io/webgeeker/full/YjrZgg/</a></p> + +<h2 id="示例">示例</h2> + +<p>以下示例改编自<a href="http://hacks.mozilla.org/2012/05/dom-mutationobserver-reacting-to-dom-changes-without-killing-browser-performance/">这篇博客</a>。</p> + +<pre class="brush: js notranslate">// 选择需要观察变动的节点 +const targetNode = document.getElementById('some-id'); + +// 观察器的配置(需要观察什么变动) +const config = { attributes: true, childList: true, subtree: true }; + +// 当观察到变动时执行的回调函数 +const callback = function(mutationsList, observer) { + // Use traditional 'for loops' for IE 11 + for(let mutation of mutationsList) { + if (mutation.type === 'childList') { + console.log('A child node has been added or removed.'); + } + else if (mutation.type === 'attributes') { + console.log('The ' + mutation.attributeName + ' attribute was modified.'); + } + } +}; + +// 创建一个观察器实例并传入回调函数 +const observer = new MutationObserver(callback); + +// 以上述配置开始观察目标节点 +observer.observe(targetNode, config); + +// 之后,可停止观察 +observer.disconnect(); +</pre> + +<ul> +</ul> + +<h2 id="Specifications" name="Specifications">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">注释</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('DOM WHATWG', '#mutationobserver', 'MutationObserver')}}</td> + <td>{{ Spec2('DOM WHATWG') }}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容">浏览器兼容</h2> + + + +<p>{{Compat("api.MutationObserver")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{domxref('PerformanceObserver')}}</li> + <li>{{domxref('ResizeObserver')}}</li> + <li>{{domxref('IntersectionObserver')}}</li> + <li><a href="http://updates.html5rocks.com/2012/02/Detect-DOM-changes-with-Mutation-Observers" rel="freelink">A brief overview</a></li> + <li><a href="http://hacks.mozilla.org/2012/05/dom-mutationobserver-reacting-to-dom-changes-without-killing-browser-performance/" rel="freelink">A more in-depth discussion</a></li> + <li><a href="http://www.youtube.com/watch?v=eRZ4pO0gVWw" rel="freelink">A screencast by Chromium developer Rafael Weinstein</a></li> +</ul> |