{"id":167960,"date":"2024-05-08T16:13:40","date_gmt":"2024-05-08T08:13:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/167960.html"},"modified":"2024-05-08T16:13:45","modified_gmt":"2024-05-08T08:13:45","slug":"javascript-%e4%b8%ad%e6%9c%89%e6%b2%a1%e6%9c%89%e6%a3%80%e6%b5%8b%e5%86%85%e5%ae%b9%e6%94%b9%e5%8f%98%e7%9a%84%e4%ba%8b%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/167960.html","title":{"rendered":"JavaScript \u4e2d\u6709\u6ca1\u6709\u68c0\u6d4b\u5185\u5bb9\u6539\u53d8\u7684\u4e8b\u4ef6"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27034009\/a058080c-3518-4da0-bcc5-b1194b8ac0b3.webp\" alt=\"JavaScript \u4e2d\u6709\u6ca1\u6709\u68c0\u6d4b\u5185\u5bb9\u6539\u53d8\u7684\u4e8b\u4ef6\" \/><\/p>\n<p><p>\u786e\u5b9e\uff0c\u5728JavaScript\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u6cd5\u6765\u68c0\u6d4b\u5185\u5bb9\u7684\u6539\u53d8\u4e8b\u4ef6\u3002<strong>\u6700\u5e38\u89c1\u7684\u5305\u62ec\u4f7f\u7528<code>MutationObserver<\/code>\u3001<code>input<\/code>\u4e8b\u4ef6\u3001\u4ee5\u53ca\u67d0\u4e9b\u60c5\u51b5\u4e0b\u7684<code>propertychange<\/code>\u4e8b\u4ef6\uff08\u4ec5\u9650IE\uff09<\/strong>\u3002\u8fd9\u4e9b\u6280\u672f\u8ba9\u6211\u4eec\u80fd\u591f\u8ffd\u8e2a\u5230DOM\u5143\u7d20\u6216\u8868\u5355\u63a7\u4ef6\u5185\u5bb9\u7684\u6539\u53d8\uff0c\u5e76\u5728\u8fd9\u4e9b\u66f4\u6539\u53d1\u751f\u65f6\u6267\u884c\u76f8\u5173\u7684\u903b\u8f91\u3002\u8ba9\u6211\u4eec\u9996\u5148\u8be6\u7ec6\u5c55\u5f00\u8bb2\u8bb2<code>MutationObserver<\/code>\u8fd9\u4e00\u6280\u672f\u3002<\/p>\n<\/p>\n<p><p><code>MutationObserver<\/code>\u662f\u4e00\u79cd\u73b0\u4ee3\u5316\u3001\u9ad8\u6027\u80fd\u7684\u65b9\u6cd5\uff0c\u7528\u4e8e\u76d1\u89c6DOM\u53d8\u5316\u3002\u5b83\u80fd\u5728\u76ee\u6807\u8282\u70b9\u6216\u6811\u4e0a\u53d1\u751f\u53d8\u5316\u65f6\u5f02\u6b65\u901a\u77e5\u4f60\uff0c\u8fd9\u5305\u62ec\u5b50\u8282\u70b9\u7684\u589e\u52a0\u6216\u5220\u9664\u3001\u5c5e\u6027\u7684\u66f4\u6539\u7b49\u3002\u4f7f\u7528<code>MutationObserver<\/code>\u975e\u5e38\u9002\u5408\u4e8e\u76d1\u6d4b\u590d\u6742\u9875\u9762\u7ed3\u6784\u4e2d\u5143\u7d20\u7684\u52a8\u6001\u6539\u53d8\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u80fd\u529b\uff0c\u5728\u4e0d\u8f6e\u8be2DOM\u7684\u60c5\u51b5\u4e0b\u68c0\u6d4b\u9875\u9762\u7684\u66f4\u6539\uff0c\u4ece\u800c\u4f18\u5316\u6027\u80fd\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001MUTATIONOBSERVER\u7684\u4f7f\u7528<\/h3>\n<\/p>\n<p><h4>\u521b\u5efa\u548c\u914d\u7f6eMutationObserver<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2a<code>MutationObserver<\/code>\u7684\u5b9e\u4f8b\uff0c\u5e76\u914d\u7f6e\u5b83\u76d1\u542cDOM\u53d8\u5316\u7684\u7c7b\u578b\u3002\u7136\u540e\uff0c\u6307\u5b9a\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u5f53\u89c2\u5bdf\u5230\u53d8\u5316\u65f6\uff0c\u8fd9\u4e2a\u51fd\u6570\u4f1a\u88ab\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">const observer = new MutationObserver((mutations) =&gt; {<\/p>\n<p>  mutations.forEach((mutation) =&gt; {<\/p>\n<p>    console.log(mutation);<\/p>\n<p>  });<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u5f00\u59cb\u89c2\u5bdf<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>observe<\/code>\u65b9\u6cd5\u5f00\u59cb\u5bf9DOM\u8282\u70b9\u7684\u6539\u53d8\u8fdb\u884c\u76d1\u89c6\u3002\u4f60\u9700\u8981\u6307\u5b9a\u76ee\u6807\u8282\u70b9\u4ee5\u53ca\u60f3\u5173\u6ce8\u7684\u53d8\u5316\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">observer.observe(document.getElementById(&#039;myElement&#039;), {<\/p>\n<p>  childList: true,<\/p>\n<p>  attributes: true,<\/p>\n<p>  subtree: true<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001INPUT\u4e8b\u4ef6<\/h3>\n<\/p>\n<p><p>\u9488\u5bf9\u8868\u5355\u5143\u7d20\uff08\u5982<code>&lt;input&gt;<\/code>\u3001<code>&lt;textarea&gt;<\/code>\uff09\uff0c\u76d1\u542c<code>input<\/code>\u4e8b\u4ef6\u662f\u68c0\u6d4b\u5185\u5bb9\u53d8\u5316\u7684\u53e6\u4e00\u79cd\u6709\u6548\u65b9\u5f0f\u3002\u5f53\u7528\u6237\u5728\u8868\u5355\u63a7\u4ef6\u4e2d\u8f93\u5165\u65f6\uff0c<code>input<\/code>\u4e8b\u4ef6\u4f1a\u88ab\u89e6\u53d1\uff0c\u8ba9\u6211\u4eec\u80fd\u591f\u6267\u884c\u67d0\u4e9b\u64cd\u4f5c\u6216\u9a8c\u8bc1\u8868\u5355\u3002<\/p>\n<\/p>\n<p><h4>\u7ed1\u5b9ainput\u4e8b\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">document.querySelector(&#039;input&#039;).addEventListener(&#039;input&#039;, function(e) {<\/p>\n<p>  console.log(&#039;Input value changed!&#039;, e.target.value);<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4f7f\u7528\u573a\u666f<\/h4>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7279\u522b\u9002\u5408\u5b9e\u65bd\u5b9e\u65f6\u8868\u5355\u9a8c\u8bc1\u6216\u641c\u7d22\u5efa\u8bae\u7b49\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001PROPERTYCHANGE\u4e8b\u4ef6\uff08\u4ec5IE\uff09<\/h3>\n<\/p>\n<p><p>\u5728\u65e9\u671f\u7684Internet Explorer\u7248\u672c\u4e2d\uff0c<code>propertychange<\/code>\u4e8b\u4ef6\u7528\u4e8e\u68c0\u6d4b\u5143\u7d20\u5c5e\u6027\u7684\u66f4\u6539\uff0c\u7c7b\u4f3c\u4e8e\u73b0\u4ee3\u6807\u51c6\u4e2d\u7684<code>input<\/code>\u4e8b\u4ef6\u3002\u5c3d\u7ba1\u73b0\u4ee3\u6d4f\u89c8\u5668\u5df2\u4e0d\u518d\u9700\u8981\u8fd9\u4e00\u4e8b\u4ef6\uff0c\u4f46\u5728\u5f00\u53d1\u9700\u8981\u652f\u6301\u8001\u7248\u672cIE\u6d4f\u89c8\u5668\u7684\u5e94\u7528\u65f6\uff0c\u4e86\u89e3\u5b83\u4ecd\u7136\u6709\u5176\u4ef7\u503c\u3002<\/p>\n<\/p>\n<p><h4>\u76d1\u542cpropertychange\u4e8b\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-javascript\">document.getElementById(&#039;myInput&#039;).attachEvent(&#039;onpropertychange&#039;, function(e) {<\/p>\n<p>  if(e.propertyName === &#039;value&#039;) {<\/p>\n<p>    console.log(&#039;Value changed!&#039;);<\/p>\n<p>  }<\/p>\n<p>});<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3\u4e0e\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><p><strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/strong>\u53d6\u51b3\u4e8e\u4f60\u9700\u8981\u76d1\u6d4b\u7684\u5185\u5bb9\u7684\u6027\u8d28\u4e0e\u76ee\u6807\u3002\u5bf9\u4e8e\u4e00\u822c\u7684DOM\u5143\u7d20\u53d8\u5316\uff0c<code>MutationObserver<\/code>\u63d0\u4f9b\u4e86\u4e00\u79cd\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u89e3\u51b3\u65b9\u6848\u3002\u800c\u5bf9\u4e8e\u8868\u5355\u5143\u7d20\u7684\u5b9e\u65f6\u76d1\u63a7\uff0c<code>input<\/code>\u4e8b\u4ef6\u5219\u66f4\u52a0\u7b80\u4fbf\u9ad8\u6548\u3002\u5728\u5904\u7406\u517c\u5bb9\u6027\u95ee\u9898\u65f6\uff0c\u7279\u522b\u662f\u9700\u8981\u652f\u6301\u65e7\u7248IE\u6d4f\u89c8\u5668\u7684\u573a\u5408\uff0c\u53ef\u80fd\u8fd8\u9700\u8981\u56de\u9000\u5230\u4f7f\u7528<code>propertychange<\/code>\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u5728\u4f7f\u7528\u8fd9\u4e9b\u6280\u672f\u65f6\uff0c\u6700\u597d\u7684\u505a\u6cd5\u662f<strong>\u4ec5\u5728\u5fc5\u8981\u65f6\u6dfb\u52a0\u76d1\u542c\u5668\uff0c\u5e76\u5728\u9002\u5f53\u65f6\u673a\u79fb\u9664\u5b83\u4eec\uff0c\u4ee5\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u6027\u80fd\u635f\u8017<\/strong>\u3002\u6b64\u5916\uff0c\u5229\u7528\u8fd9\u4e9b\u4e8b\u4ef6\u54cd\u5e94\u51fd\u6570\u65f6\uff0c\u5e94\u5c3d\u91cf\u786e\u4fdd\u4ee3\u7801\u7684\u9ad8\u6548\u548c\u7b80\u6d01\uff0c\u907f\u514d\u5728\u8fd9\u4e9b\u51fd\u6570\u4e2d\u6267\u884c\u590d\u6742\u6216\u8017\u65f6\u7684\u64cd\u4f5c\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u4e0d\u4ec5\u80fd\u591f\u6709\u6548\u76d1\u63a7\u5185\u5bb9\u7684\u53d8\u5316\uff0c\u8fd8\u80fd\u4fdd\u8bc1\u5e94\u7528\u7684\u54cd\u5e94\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728 JavaScript \u4e2d\u76d1\u542c\u8f93\u5165\u6846\u5185\u5bb9\u7684\u53d8\u5316\uff1f<\/strong><\/p>\n<p>\u53ef\u4ee5\u501f\u52a9\u4e8b\u4ef6\u76d1\u542c\u5668\u6765\u5b9e\u73b0\u3002\u901a\u8fc7\u4f7f\u7528 <code>input<\/code> \u4e8b\u4ef6\uff0c\u53ef\u4ee5\u76d1\u542c\u7528\u6237\u5728\u8f93\u5165\u6846\u4e2d\u8f93\u5165\u3001\u7c98\u8d34\u6216\u8005\u5220\u9664\u5185\u5bb9\u7684\u884c\u4e3a\u3002\u5f53\u5185\u5bb9\u53d1\u751f\u6539\u53d8\u65f6\uff0c\u89e6\u53d1\u8be5\u4e8b\u4ef6\uff0c\u5e76\u4e14\u53ef\u4ee5\u83b7\u53d6\u5230\u8f93\u5165\u6846\u7684\u6700\u65b0\u503c\u3002<\/p>\n<pre><code class=\"language-javascript\">const inputElement = document.getElementById(&#039;myInput&#039;);\n\ninputElement.addEventListener(&#039;input&#039;, function() {\n  const updatedValue = inputElement.value;\n  console.log(&#039;\u8f93\u5165\u6846\u5185\u5bb9\u5df2\u6539\u53d8\uff1a&#039;, updatedValue);\n});\n<\/code><\/pre>\n<p><strong>2. \u5982\u4f55\u68c0\u6d4b JavaScript \u4e2d DOM \u5143\u7d20\u7684\u5185\u5bb9\u662f\u5426\u53d1\u751f\u6539\u53d8\uff1f<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 <code>MutationObserver<\/code> API \u6765\u68c0\u6d4b DOM \u5143\u7d20\u5185\u5bb9\u7684\u6539\u53d8\u3002<code>MutationObserver<\/code> \u53ef\u4ee5\u89c2\u5bdf\u5e76\u54cd\u5e94 DOM \u7684\u53d8\u5316\uff0c\u5305\u62ec\u5143\u7d20\u5185\u5bb9\u7684\u4fee\u6539\u3001\u5c5e\u6027\u7684\u53d8\u5316\u7b49\u3002<\/p>\n<pre><code class=\"language-javascript\">const targetElement = document.getElementById(&#039;target&#039;);\n\nconst mutationObserver = new MutationObserver(function(mutations) {\n  mutations.forEach(function(mutation) {\n    if (mutation.type === &#039;characterData&#039;) {\n      const updatedContent = mutation.target.data;\n      console.log(&#039;\u5143\u7d20\u5185\u5bb9\u5df2\u6539\u53d8\uff1a&#039;, updatedContent);\n    }\n  });\n});\n\nconst observerConfig = {\n  characterData: true,\n  subtree: true\n};\n\nmutationObserver.observe(targetElement, observerConfig);\n<\/code><\/pre>\n<p><strong>3. \u5982\u4f55\u76d1\u542c JavaScript \u4e2d\u6570\u7ec4\u7684\u5185\u5bb9\u6539\u53d8\uff1f<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 <code>Object.defineProperty<\/code> \u65b9\u6cd5\u6765\u76d1\u542c JavaScript \u4e2d\u6570\u7ec4\u7684\u5185\u5bb9\u6539\u53d8\u3002\u901a\u8fc7\u91cd\u65b0\u5b9a\u4e49\u6570\u7ec4\u7684 <code>push<\/code>\u3001<code>pop<\/code>\u3001<code>shift<\/code>\u3001<code>unshift<\/code>\u3001<code>splice<\/code> \u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u6570\u7ec4\u5185\u5bb9\u53d1\u751f\u6539\u53d8\u65f6\u6267\u884c\u6307\u5b9a\u7684\u56de\u8c03\u51fd\u6570\u3002<\/p>\n<pre><code class=\"language-javascript\">const myArray = [];\n\nObject.defineProperty(myArray, &#039;push&#039;, {\n  configurable: true,\n  enumerable: false,\n  writable: true,\n  value: function(...args) {\n    const result = Array.prototype.push.apply(this, args);\n    console.log(&#039;\u6570\u7ec4\u5185\u5bb9\u5df2\u6539\u53d8\uff1a&#039;, this);\n    return result;\n  }\n});\n\nmyArray.push(&#039;\u65b0\u5143\u7d20&#039;);\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u786e\u5b9e\uff0c\u5728JavaScript\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u6cd5\u6765\u68c0\u6d4b\u5185\u5bb9\u7684\u6539\u53d8\u4e8b\u4ef6\u3002\u6700\u5e38\u89c1\u7684\u5305\u62ec\u4f7f\u7528MutationObse [&hellip;]","protected":false},"author":3,"featured_media":167966,"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\/167960"}],"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=167960"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/167960\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/167966"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=167960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=167960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=167960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}