MutationObserver.observe() - Web APIs 编辑

The MutationObserver method observe() configures the MutationObserver callback to begin receiving notifications of changes to the DOM that match the given options. Depending on the configuration, the observer may watch a single Node in the DOM tree, or that node and some or all of its descendant nodes.

To stop the MutationObserver (so that none of its callbacks will be triggered any longer), call MutationObserver.disconnect().

    Syntax

    mutationObserver.observe(target, options)
    

    Parameters

    target
    A DOM Node (which may be an Element) within the DOM tree to watch for changes, or to be the root of a subtree of nodes to be watched.
    options
    MutationObserverInit object providing options that describe which DOM mutations should be reported to mutationObserver’s callback. At a minimum, one of childList, attributes, and/or characterData must be true when you call observe(). Otherwise, a TypeError exception will be thrown.
    Options are as follows:
    subtree Optional
    Set to true to extend monitoring to the entire subtree of nodes rooted at target. All of the other MutationObserverInit properties are then extended to all of the nodes in the subtree instead of applying solely to the target node. The default value is false.
    childList Optional
    Set to true to monitor the target node (and, if subtree is true, its descendants) for the addition of new child nodes or removal of existing child nodes. The default value is false.
    attributes Optional
    Set to true to watch for changes to the value of attributes on the node or nodes being monitored. The default value is true if either of attributeFilter or attributeOldValue is specified, otherwise the default value is false.
    attributeFilter Optional
    An array of specific attribute names to be monitored. If this property isn't included, changes to all attributes cause mutation notifications.
    attributeOldValue Optional
    Set to true to record the previous value of any attribute that changes when monitoring the node or nodes for attribute changes; see Monitoring attribute values in MutationObserver for details on watching for attribute changes and value recording. The default value is false.
    characterData Optional
    Set to true to monitor the specified target node (and, if subtree is true, its descendants) for changes to the character data contained within the node or nodes. The default value is true if characterDataOldValue is specified, otherwise the default value is false.
    characterDataOldValue Optional
    Set to true to record the previous value of a node's text whenever the text changes on nodes being monitored. For details and an example, see Monitoring text content changes in MutationObserver. The default value is false.

    Return value

    undefined.

    Exceptions

    TypeError
    Thrown in any of the following circumstances:

    Usage notes

    Reusing MutationObservers

    You can call observe() multiple times on the same MutationObserver to watch for changes to different parts of the DOM tree and/or different types of changes. There are some caveats to note:

    • If you call observe() on a node that's already being observed by the same MutationObserver, all existing observers are automatically removed from all targets being observed before the new observer is activated.
    • If the same MutationObserver is not already in use on the target, then the existing observers are left alone and the new one is added.

    Observation follows nodes when disconnected

    Mutation observers are intended to let you be able to watch the desired set of nodes over time, even if the direct connections between those nodes are severed. If you begin watching a subtree of nodes, and a portion of that subtree is detached and moved elsewhere in the DOM, you continue to watch the detached segment of nodes, receiving the same callbacks as before the nodes were detached from the original subtree.

    In other words, until you've been notified that nodes are being split off from your monitored subtree, you'll get notifications of changes to that split-off subtree and its nodes. This prevents you from missing changes that occur after the connection is severed and before you have a chance to specifically begin monitoring the moved node or subtree for changes.

    Theoretically, this means that if you keep track of the MutationRecord objects describing the changes that occur, you should be able to "undo" the changes, rewinding the DOM back to its initial state.

    Example

    In this example, we demonstrate how to call the method observe() on an instance of MutationObserver, once it has been set up, passing it a target element and a MutationObserverInit options object.

    // identify an element to observe
    const elementToObserve = document.querySelector("#targetElementId");
    
    // create a new instance of `MutationObserver` named `observer`,
    // passing it a callback function
    const observer = new MutationObserver(function() {
        console.log('callback that runs when observer is triggered');
    });
    
    // call `observe()` on that MutationObserver instance,
    // passing it the element to observe, and the options object
    observer.observe(elementToObserve, {subtree: true, childList: true});

    Specifications

    SpecificationStatusComment
    DOM
    The definition of 'MutationObserver.observe()' in that specification.
    Living Standard

    Browser compatibility

    BCD tables only load in the browser

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
    列表为空,暂无数据

    词条统计

    浏览:94 次

    字数:10796

    最后编辑:6 年前

    编辑次数:0 次

      我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
      原文