{"id":88301,"date":"2024-04-07T17:27:58","date_gmt":"2024-04-07T09:27:58","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/88301.html"},"modified":"2024-04-07T17:27:59","modified_gmt":"2024-04-07T09:27:59","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8mutationobserver%e7%9b%91%e8%a7%86dom%e5%8f%98%e5%8c%96","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/88301.html","title":{"rendered":"\u5982\u4f55\u4f7f\u7528MutationObserver\u76d1\u89c6DOM\u53d8\u5316"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/02172654\/a18b9cc7-b521-4e8e-aa2b-15316433762c.webp\" alt=\"\u5982\u4f55\u4f7f\u7528MutationObserver\u76d1\u89c6DOM\u53d8\u5316\" \/><\/p>\n<p><p>\u4f7f\u7528MutationObserver\u76d1\u89c6DOM\u53d8\u5316\u7684\u65b9\u6cd5\u6709\u51e0\u4e2a\u6b65\u9aa4\uff1a\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2aMutationObserver\u7684\u5b9e\u4f8b\u3001\u5176\u6b21\uff0c\u5b9a\u4e49\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u4ee5\u4fbf\u5728\u68c0\u6d4b\u5230DOM\u66f4\u6539\u65f6\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\u3001\u518d\u6b21\uff0c\u4f7f\u7528\u914d\u7f6e\u5bf9\u8c61\u6307\u5b9a\u89c2\u5bdf\u8005\u5e94\u8be5\u89c2\u5bdf\u7684DOM\u53d8\u5316\u7c7b\u578b\u3001\u6700\u540e\uff0c\u5c06\u89c2\u5bdf\u8005\u4e0e\u7279\u5b9a\u7684DOM\u8282\u70b9\u5173\u8054\u5e76\u5f00\u59cb\u89c2\u5bdf\u53d8\u5316\u3002<strong>MutationObserver<\/strong>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Web API\uff0c\u5b83\u80fd\u591f\u8ba9\u5f00\u53d1\u8005<strong>\u76d1\u542cDOM\u6811\u7684\u53d8\u5316<\/strong>\uff0c\u5305\u62ec\u5c5e\u6027\u7684\u53d8\u5316\u3001\u8282\u70b9\u7684\u589e\u52a0\u548c\u79fb\u9664\u3001\u6587\u672c\u7684\u6539\u53d8\u7b49\u3002\u8fd9\u4e2a\u5de5\u5177\u7279\u522b\u9002\u5408\u5f00\u53d1\u590d\u6742\u7684web\u5e94\u7528\u7a0b\u5e8f\uff0c\u5176\u4e2d\u7684\u4e00\u4e2a\u5173\u952e\u5e94\u7528\u573a\u666f\u662f\u5f53\u9700\u8981\u5904\u7406\u7531\u7b2c\u4e09\u65b9\u811a\u672c\u6216\u8005\u7528\u6237\u4e0e\u9875\u9762\u4ea4\u4e92\u5bfc\u81f4\u7684DOM\u53d8\u5316\u65f6\u3002\u901a\u8fc7\u4f7f\u7528MutationObserver\uff0c\u5f00\u53d1\u8005\u80fd\u591f\u53ca\u65f6\u54cd\u5e94DOM\u7684\u6539\u53d8\uff0c\u5e76\u6267\u884c\u4e00\u4e9b\u5fc5\u8981\u7684\u903b\u8f91\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u521b\u5efaMUTATIONOBSERVER\u5b9e\u4f8b<\/h3>\n<\/p>\n<p><p>\u8981\u4f7f\u7528MutationObserver\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u5b83\u7684\u4e00\u4e2a\u5b9e\u4f8b\u3002\u8fd9\u4e2a\u5b9e\u4f8b\u5c06\u4f1a\u9644\u5e26\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u8be5\u56de\u8c03\u51fd\u6570\u5c06\u5728\u6bcf\u6b21DOM\u53d8\u5316\u65f6\u88ab\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const observer = new MutationObserver(callback);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c<code>callback<\/code>\u662f\u5f53\u89c2\u5bdf\u7684DOM\u53d1\u751f\u53d8\u5316\u65f6\uff0cMutationObserver\u5c06\u8981\u6267\u884c\u7684\u51fd\u6570\u3002\u8fd9\u4e2a\u56de\u8c03\u53ef\u4ee5\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff1amutations\u548cobserver\u3002\u5176\u4e2d<code>mutations<\/code>\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u5305\u542b\u4e86\u6240\u6709\u88ab\u89c2\u5bdf\u7684DOM\u53d8\u5316\u4e4b\u60c5\u62a5\uff0c\u800c<code>observer<\/code>\u5c31\u662fMutationObserver\u5b9e\u4f8b\u672c\u8eab\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b9a\u4e49\u56de\u8c03\u51fd\u6570<\/h3>\n<\/p>\n<p><pre><code class=\"language-javascript\">function callback(mutationsList, observer) {<\/p>\n<p>    for (let mutation of mutationsList) {<\/p>\n<p>        if (mutation.type === &#039;childList&#039;) {<\/p>\n<p>            console.log(&#039;A child node has been added or removed.&#039;);<\/p>\n<p>        } else if (mutation.type === &#039;attributes&#039;) {<\/p>\n<p>            console.log(`The ${mutation.attributeName} attribute was modified.`);<\/p>\n<p>        }<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u56de\u8c03\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u5224\u65ad<code>mutation.type<\/code>\u7684\u503c\u6765\u786e\u5b9a\u89c2\u5bdf\u6240\u53d1\u73b0\u7684\u53d8\u5316\u7c7b\u578b\uff0c\u5e76\u6267\u884c\u76f8\u5e94\u64cd\u4f5c\u3002Mutation\u7c7b\u578b\u5305\u62ec&quot;childList&quot;\uff08\u5b50\u8282\u70b9\u7684\u53d8\u66f4\uff09\u3001&quot;attributes&quot;\uff08\u5c5e\u6027\u7684\u53d8\u66f4\uff09\u548c&quot;characterData&quot;\uff08\u6587\u672c\u5185\u5bb9\u7684\u53d8\u66f4\uff09\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u914d\u7f6eOBSERVER<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u4e86observer\u5b9e\u4f8b\u5e76\u5b9a\u4e49\u4e86\u56de\u8c03\u51fd\u6570\u540e\uff0c\u5fc5\u987b\u914d\u7f6eobserver\u4ee5\u544a\u8bc9\u5b83\u5e94\u8be5\u76d1\u542c\u54ea\u4e9b\u7c7b\u578b\u7684DOM\u53d8\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const config = {<\/p>\n<p>    attributes: true,<\/p>\n<p>    childList: true,<\/p>\n<p>    subtree: true<\/p>\n<p>};<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u914d\u7f6e\u5bf9\u8c61\u8bf4\u660eobserver\u5c06\u4f1a\u76d1\u542c\u5c5e\u6027\u7684\u53d8\u5316\u3001\u5b50\u8282\u70b9\u7684\u589e\u51cf\u4ee5\u53ca\u8fd9\u4e9b\u53d8\u5316\u662f\u5426\u5728\u76ee\u6807\u8282\u70b9\u7684\u540e\u4ee3\u5143\u7d20\u4e2d\u53d1\u751f\u3002\u6bcf\u4e00\u9879\u90fd\u662f\u53ef\u9009\u7684\uff0c\u6309\u9700\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5173\u8054DOM\u8282\u70b9\u5e76\u5f00\u59cb\u89c2\u5bdf<\/h3>\n<\/p>\n<p><p>\u73b0\u5728\u6211\u4eec\u6709\u4e86\u4e00\u4e2a\u914d\u7f6e\u597d\u7684\u89c2\u5bdf\u8005\u5b9e\u4f8b\uff0c\u53ef\u4ee5\u5c06\u5b83\u4e0eDOM\u7684\u76ee\u6807\u8282\u70b9\u5173\u8054\u8d77\u6765\uff0c\u5e76\u5f00\u59cb\u76d1\u542c\u53d8\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const targetNode = document.getElementById(&#039;some-id&#039;);<\/p>\n<p>observer.observe(targetNode, config);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>targetNode<\/code>\u662f\u8981\u76d1\u89c6\u7684DOM\u5143\u7d20\uff0c\u800c<code>config<\/code>\u5c31\u662f\u4e0a\u9762\u5b9a\u4e49\u7684\u914d\u7f6e\u5bf9\u8c61\u3002\u8c03\u7528<code>observe<\/code>\u65b9\u6cd5\u540e\uff0c\u5c31\u4f1a\u5f00\u59cb\u76d1\u89c6\u6307\u5b9a\u8282\u70b9\u7684\u53d8\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u54cd\u5e94DOM\u53d8\u5316<\/h3>\n<\/p>\n<p><p>\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u56de\u8c03\u51fd\u6570\u4e2d\u6839\u636e\u5e94\u7528\u9700\u8981\u6267\u884c\u4efb\u4f55\u79cd\u7c7b\u7684\u64cd\u4f5c\u3002\u5e38\u89c1\u7684\u5e94\u7528\u5305\u62ec\u540c\u6b65\u66f4\u65b0\u7528\u6237\u754c\u9762\u3001\u89e6\u53d1\u5176\u4ed6\u811a\u672c\u7684\u6267\u884c\u6216\u8005 sending notifications\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u505c\u6b62\u89c2\u5bdf<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u5982\u679c\u4e0d\u518d\u9700\u8981\u76d1\u89c6DOM\u53d8\u5316\uff0c\u6216\u8005\u60f3\u8981\u79fb\u9664observer\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\uff0c\u53ef\u4ee5\u8c03\u7528<code>disconnect<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">observer.disconnect();<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6b64\u65b9\u6cd5\u4f1a\u963b\u6b62observer\u5b9e\u4f8b\u76d1\u542c\u4efb\u4f55\u66f4\u591a\u7684\u53d8\u5316\uff0c\u5141\u8bb8\u5b83\u88ab\u5783\u573e\u56de\u6536\u3002<\/p>\n<\/p>\n<p><p>MutationObserver\u975e\u5e38\u6709\u7528\u4f46\u4e5f\u8981\u8c28\u614e\u4f7f\u7528\uff0c\u56e0\u4e3a\u5982\u679c\u76d1\u89c6\u8fc7\u4e8e\u5e7f\u6cdb\u7684DOM\u53d8\u5316\uff0c\u53ef\u80fd\u4f1a\u5f71\u54cd\u5e94\u7528\u7684\u6027\u80fd\u3002\u5728\u4f7f\u7528\u65f6\u8981\u8003\u8651\u5230\u6027\u80fd\u7684\u6743\u8861\uff0c\u5e76\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\u548c\u7ec6\u81f4\u7684\u914d\u7f6e\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u7cbe\u786e\u5730\u4f7f\u7528MutationObserver\u76d1\u89c6DOM\u7684\u53d8\u5316\uff0c\u4ece\u800c\u5728web\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u52a8\u6001\u548c\u54cd\u5e94\u5f0f\u7684\u7528\u6237\u4f53\u9a8c\u3002\u5b83\u662f\u73b0\u4ee3JavaScript\u5de5\u5177\u7bb1\u4e2d\u7684\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u7ec4\u6210\u90e8\u5206\uff0c\u4e3a\u5904\u7406\u590d\u6742\u7684DOM\u4ea4\u4e92\u63d0\u4f9b\u4e86\u6781\u5927\u7684\u7075\u6d3b\u6027\u548c\u63a7\u5236\u80fd\u529b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u95ee\u98981\uff1aMutationObserver\u662f\u4ec0\u4e48\uff1f<\/strong><\/p>\n<p>MutationObserver\u662fWeb API\u4e2d\u7684\u4e00\u4e2a\u63a5\u53e3\uff0c\u5b83\u53ef\u4ee5\u7528\u6765\u5f02\u6b65\u76d1\u89c6DOM\u6811\u7684\u53d8\u5316\u3002\u5f53DOM\u6811\u7684\u67d0\u4e2a\u8282\u70b9\u88ab\u6dfb\u52a0\u3001\u5220\u9664\u6216\u5c5e\u6027\u53d1\u751f\u53d8\u5316\u65f6\uff0cMutationObserver\u53ef\u4ee5\u89e6\u53d1\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u6765\u5904\u7406\u8fd9\u4e9b\u53d8\u5316\u3002<\/p>\n<p>MutationObserver\u7684\u4f18\u70b9\u662f\u5b83\u53ef\u4ee5\u7cbe\u786e\u5730\u901a\u8fc7\u56de\u8c03\u51fd\u6570\u6355\u6349\u5230DOM\u7684\u53d8\u5316\uff0c\u5e76\u4e14\u5728\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u5904\u7406\u8fd9\u4e9b\u53d8\u5316\uff0c\u4e0d\u4f1a\u963b\u585e\u9875\u9762\u7684\u5176\u4ed6\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u95ee\u98982\uff1a\u5982\u4f55\u4f7f\u7528MutationObserver\u76d1\u89c6DOM\u7684\u53d8\u5316\uff1f<\/strong><\/p>\n<p>\u8981\u4f7f\u7528MutationObserver\u6765\u76d1\u89c6DOM\u7684\u53d8\u5316\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2aMutationObserver\u5b9e\u4f8b\uff0c\u5e76\u4f20\u5165\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u3002\u8be5\u56de\u8c03\u51fd\u6570\u4f1a\u5728DOM\u6811\u53d1\u751f\u53d8\u5316\u65f6\u88ab\u89e6\u53d1\u3002<\/p>\n<p>\u5728\u56de\u8c03\u51fd\u6570\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u89c2\u5bdf\u5668\u5bf9\u8c61\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u6765\u83b7\u53d6DOM\u6811\u7684\u53d8\u5316\u4fe1\u606f\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528observe\u65b9\u6cd5\u6765\u6307\u5b9a\u8981\u76d1\u89c6\u7684\u76ee\u6807\u8282\u70b9\uff0c\u4ee5\u53ca\u6240\u5173\u6ce8\u7684\u53d8\u5316\u7c7b\u578b\u3002\u8fd8\u53ef\u4ee5\u4f7f\u7528disconnect\u65b9\u6cd5\u505c\u6b62\u76d1\u89c6\u3002<\/p>\n<p>\u5f53DOM\u6811\u7684\u53d8\u5316\u7b26\u5408\u6211\u4eec\u6307\u5b9a\u7684\u6761\u4ef6\u65f6\uff0c\u56de\u8c03\u51fd\u6570\u4f1a\u88ab\u89e6\u53d1\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u56de\u8c03\u51fd\u6570\u4e2d\u5904\u7406\u8fd9\u4e9b\u53d8\u5316\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8fdb\u884c\u4e00\u4e9b\u9488\u5bf9DOM\u53d8\u5316\u7684\u64cd\u4f5c\uff0c\u6216\u8005\u66f4\u65b0\u9875\u9762\u7684\u5176\u4ed6\u90e8\u5206\u3002<\/p>\n<p><strong>\u95ee\u98983\uff1a\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u4f7f\u7528MutationObserver\uff1f<\/strong><\/p>\n<p>MutationObserver\u9002\u7528\u4e8e\u9700\u8981\u76d1\u89c6DOM\u6811\u53d8\u5316\u7684\u573a\u666f\uff0c\u6bd4\u5982\u4ee5\u4e0b\u60c5\u51b5\uff1a<\/p>\n<ol>\n<li>\n<p>\u52a8\u6001\u6dfb\u52a0\u6216\u5220\u9664\u8282\u70b9\uff1a\u5f53\u9875\u9762\u4e2d\u7684\u8282\u70b9\u4f1a\u52a8\u6001\u5730\u88ab\u6dfb\u52a0\u6216\u5220\u9664\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528MutationObserver\u6765\u76d1\u89c6\u8fd9\u4e9b\u53d8\u5316\uff0c\u5e76\u505a\u51fa\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/p>\n<\/li>\n<li>\n<p>\u5c5e\u6027\u53d8\u5316\u7684\u76d1\u542c\uff1a\u5982\u679c\u9700\u8981\u76d1\u542cDOM\u5143\u7d20\u7684\u5c5e\u6027\u662f\u5426\u53d1\u751f\u53d8\u5316\uff0c\u53ef\u4ee5\u4f7f\u7528MutationObserver\u6765\u76d1\u89c6\u5c5e\u6027\u7684\u53d8\u5316\uff0c\u5e76\u5728\u53d8\u5316\u65f6\u89e6\u53d1\u76f8\u5e94\u7684\u64cd\u4f5c\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f02\u6b65\u64cd\u4f5c\uff1a\u5f53\u67d0\u4e9b\u64cd\u4f5c\u662f\u5f02\u6b65\u7684\uff0c\u5e76\u4e14\u9700\u8981\u7b49\u5f85DOM\u6811\u7684\u72b6\u6001\u53d8\u5316\u540e\u518d\u6267\u884c\u65f6\uff0cMutationObserver\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5728\u6070\u5f53\u7684\u65f6\u673a\u89e6\u53d1\u8fd9\u4e9b\u64cd\u4f5c\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u4f7f\u7528MutationObserver\u53ef\u4ee5\u8ba9\u6211\u4eec\u66f4\u7cbe\u786e\u5730\u6355\u6349DOM\u6811\u7684\u53d8\u5316\uff0c\u5e76\u6839\u636e\u53d8\u5316\u505a\u51fa\u76f8\u5e94\u7684\u5904\u7406\uff0c\u63d0\u9ad8\u9875\u9762\u7684\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528MutationObserver\u76d1\u89c6DOM\u53d8\u5316\u7684\u65b9\u6cd5\u6709\u51e0\u4e2a\u6b65\u9aa4\uff1a\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2aMutationObserv [&hellip;]","protected":false},"author":3,"featured_media":88304,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/88301"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=88301"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/88301\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/88304"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=88301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=88301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=88301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}