{"id":9949,"date":"2021-05-13T20:17:10","date_gmt":"2021-05-13T12:17:10","guid":{"rendered":"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9949"},"modified":"2021-05-13T20:17:10","modified_gmt":"2021-05-13T12:17:10","slug":"js-value-change","status":"publish","type":"post","link":"https:\/\/www.zhangxinxu.com\/wordpress\/2021\/05\/js-value-change\/","title":{"rendered":"\u8f93\u5165\u6846value\u5c5e\u6027\u8d4b\u503c\u89e6\u53d1js change\u4e8b\u4ef6\u7684\u5b9e\u73b0"},"content":{"rendered":"<p>by <a href=\"https:\/\/www.zhangxinxu.com\/\">zhangxinxu<\/a> from <a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9949\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9949<\/a><br \/>\n\u672c\u6587\u6b22\u8fce\u5206\u4eab\u4e0e\u805a\u5408\uff0c\u5168\u6587\u8f6c\u8f7d\u5c31\u4e0d\u5fc5\u4e86\uff0c\u5c0a\u91cd\u7248\u6743\uff0c\u5708\u5b50\u5c31\u8fd9\u4e48\u5927\uff0c\u82e5\u6025\u7528\u53ef\u4ee5\u8054\u7cfb\u6388\u6743\u3002<\/p>\n<p><script>window.wxShareImgUrl = 'https:\/\/www.zhangxinxu.com\/study\/image\/share\/202105-value-change.png';<\/script><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202105\/value-change-cover.png\" width=\"480\" height=\"300\" alt=\"\u706b\u7bad\u98de\u789f\" class=\"alignright clip thumb size-medium\" \/><\/p>\n<h3>\u4e00\u3001\u539f\u751f\u7684\u884c\u4e3a\u8868\u73b0<\/h3>\n<p>\u4f8b\u5982\u6709\u4e00\u4e2a\u5f88\u666e\u901a\u7684\u8f93\u5165\u6846\uff0cHTML\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>&lt;input id=\"input\"&gt;<\/pre>\n<p>\u6b64\u65f6\uff0c\u7528\u6237\u8f93\u5165\u5185\u5bb9\uff0c\u6216\u8005\u7c98\u8d34\u5185\u5bb9\uff0c\u6216\u8005\u62d6\u62fd\u5185\u5bb9\u5230\u8f93\u5165\u6846\uff0c\u90fd\u4f1a\u89e6\u53d1input\u4e8b\u4ef6\uff0c\u5931\u7126\u540e\u4f1a\u89e6\u53d1change\u4e8b\u4ef6\u3002<\/p>\n<p>\u4f46\u662f\u6709\u4e00\u4e2a\u662f\u4f8b\u5916\uff0c\u90a3\u5c31\u662f\u5982\u679c\u4f7f\u7528value\u5c5e\u6027\u8d4b\u503c\uff0c\u65e0\u8bba\u662finput\u4e8b\u4ef6\u8fd8\u662fchange\u4e8b\u4ef6\u90fd\u4e0d\u4f1a\u89e6\u53d1\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>input.value = '\u5f20\u946b\u65ed';  <span style=\"color:green;\">\/\/ \u4e0d\u4f1a\u89e6\u53d1change\u4e8b\u4ef6<\/span><\/pre>\n<p>\u9700\u8981\u6211\u4eec\u624b\u52a8\u89e6\u53d1\u4e0bchange\u4e8b\u4ef6\uff0c\u793a\u610f\uff1a<\/p>\n<pre>input.value = 'zhangxinxu';\r\n<span style=\"color:green;\">\/\/ \u624b\u52a8\u89e6\u53d1change\u4e8b\u4ef6<\/span>\r\ninput.dispatchEvent(new CustomEvent('change'));<\/pre>\n<p>\u663e\u7136\uff0c\u8fd9\u6837\u7684\u5904\u7406\u5c31\u5f88\u5570\u55e6\uff0c<code>dispatchEvent<\/code>\uff0c<code>CustomEvent<\/code>\u8fd9\u6837\u7684API\u5bf9\u4e0d\u5c11\u5f00\u53d1\u8005\u800c\u8a00\u662f\u964c\u751f\u7684\u3002<\/p>\n<p>\u90a3\u6709\u6ca1\u6709\u529e\u6cd5\u76f4\u63a5value\u8d4b\u503c\u4e5f\u80fd\u89e6\u53d1change\u4e8b\u4ef6\u5462\uff1f<\/p>\n<h3>\u4e8c\u3001\u81ea\u5b9a\u4e49value\u5c5e\u6027<\/h3>\n<p>\u6211\u4eec\u7ec4\u7684<a href=\"https:\/\/github.com\/XboxYan\" rel=\"noopener\" target=\"_blank\">XboxYan (\u4e25\u6587\u5f6c)<\/a>\u63d0\u4f9b\u4e86\u4e00\u79cd\u89e3\u51b3\u65b9\u6848\uff0c\u5c31\u662f\u4f7f\u7528<code>getOwnPropertyDescriptor<\/code>\u65b9\u6cd5\u5148\u628a\u6d4f\u89c8\u5668\u539f\u751f\u7684value\u8d4b\u503c\u65b9\u6cd5\u5b58\u4e0b\u6765\uff0c\u7136\u540e\u91cd\u65b0\u81ea\u5b9a\u4e49value\u8d4b\u503c\u65b9\u6cd5\uff0c\u5728\u91cd\u65b0\u81ea\u5b9a\u4e49\u7684value\u5c5e\u6027\u7684set\u65b9\u6cd5\u4e2d\u6ce8\u5165change\u4e8b\u4ef6\u7684\u89e6\u53d1\u3002<\/p>\n<p>JS\u4ee3\u7801\u793a\u610f\u5982\u4e0b\uff1a<\/p>\n<pre>const props = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value');\r\nObject.defineProperty(HTMLInputElement.prototype, 'value', {\r\n    ...props,\r\n    set (v) {\r\n        let oldv = this.value;\r\n        props.set.call(this, v);\r\n        <span style=\"color:green;\">\/\/ \u624b\u52a8\u89e6\u53d1change\u4e8b\u4ef6<\/span>\r\n        if (oldv !== v) {\r\n              input.dispatchEvent(new CustomEvent('change'));\r\n        }\r\n    }\r\n});<\/pre>\n<p>\u6211\u4eec\u4e0d\u59a8\u6d4b\u8bd5\u4e0b\uff0c\u6709\u5982\u4e0bHTML\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;input id=\"input\"&gt;\r\n&lt;p id=\"result\"&gt;&lt;\/p&gt;<\/pre>\n<p>\u7136\u540e\u6211\u4eec\u7ed1\u5b9achange\u4e8b\u4ef6\uff0c\u7136\u540e\u518d\u6267\u884cvalue\u8d4b\u503c\u65b9\u6cd5\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>input.addEventListener('change', function () {\r\n  result.innerHTML += this.value;\r\n});\r\n\r\ninput.value = 'zhangxinxu';<\/pre>\n<p>\u8fd0\u884c\u540e\u53ef\u4ee5\u770b\u5230\u4e0b\u9762\u622a\u56fe\u6240\u793a\u7684\u6548\u679c\uff0c<code>p#result<\/code>\u5143\u7d20\u5185\u81ea\u52a8\u586b\u5145\u4e86 &#8216;zhangxinxu&#8217; \u8fd9\u51e0\u4e2a\u5b57\u7b26\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202105\/2021-05-13_174200.png\" width=\"217\" height=\"89\" alt=\"\u8fd0\u884c\u7ed3\u679c\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<h3>\u4e09\u3001\u5173\u4e8egetOwnPropertyDescriptor<\/h3>\n<p>Object.getOwnPropertyDescriptor() \u65b9\u6cd5\u8fd4\u56de\u6307\u5b9a\u5bf9\u8c61\u4e0a\u4e00\u4e2a\u81ea\u6709\u5c5e\u6027\u5bf9\u5e94\u7684\u5c5e\u6027\u63cf\u8ff0\u7b26\u3002<\/p>\n<p>\u6240\u8c13\u81ea\u6709\u5c5e\u6027\uff0c\u6307\u7684\u662f\u76f4\u63a5\u8d4b\u4e88\u8be5\u5bf9\u8c61\u7684\u5c5e\u6027\uff0c\u4e0d\u9700\u8981\u4ece\u539f\u578b\u94fe\u4e0a\u8fdb\u884c\u67e5\u627e\u7684\u5c5e\u6027\u3002<\/p>\n<p>\u5c5e\u6027\u63cf\u8ff0\u7b26\u4e5f\u662f\u4e2a\u5bf9\u8c61\uff0c\u82f1\u6587\u53eb\u505aproperty descriptor\uff0c\u5c5e\u6027\u63cf\u8ff0\u7b26\u4e5f\u5305\u542b\u8bbf\u95ee\u8bbf\u95ee\u5668\u51fd\u6570\uff08getter\uff09\u7684get\u65b9\u6cd5\u548c\u83b7\u53d6\u8bbe\u7f6e\u5668\u51fd\u6570\uff08setter\uff09\u7684set\u65b9\u6cd5\uff0c\u56e0\u6b64\uff0c\u4e0a\u4e00\u8282\u793a\u610f\u4ee3\u7801\u4e2d\u7684<code>props.set<\/code>\u5176\u5b9e\u6307\u7684\u5c31\u662f\u6d4f\u89c8\u5668\u539f\u751f\u7684value\u8d4b\u503c\u65f6\u5019\u7684\u8bbe\u7f6e\u5668\u51fd\u6570\uff0c\u5305\u542b\u4e86\u6d4f\u89c8\u5668\u539f\u751f\u7684\u884c\u4e3a\u3002<\/p>\n<p>\u5c5e\u6027\u63cf\u8ff0\u7b26\u5bf9\u8c61\u652f\u6301\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u5305\u62ec\uff1a<\/p>\n<dl>\n<dt><strong>value<\/strong><\/dt>\n<dd>\u8be5\u5c5e\u6027\u7684\u503c\u3002<\/dd>\n<dt><strong>get<\/strong><\/dt>\n<dd>\u83b7\u53d6\u8be5\u5c5e\u6027\u7684\u8bbf\u95ee\u5668\u51fd\u6570\uff0c\u6ca1\u6709\u8fd4\u56deundefined\u3002<\/dd>\n<dt><strong>set<\/strong><\/dt>\n<dd>\u83b7\u53d6\u8be5\u5c5e\u6027\u7684\u8bbe\u7f6e\u5668\u51fd\u6570\uff0c\u6ca1\u6709\u8fd4\u56deundefined\u3002<\/dd>\n<dt><strong>writable<\/strong><\/dt>\n<dd>\u5f53\u4e14\u4ec5\u5f53\u5c5e\u6027\u7684\u503c\u53ef\u4ee5\u88ab\u6539\u53d8\u65f6\u4e3atrue\u3002<\/dd>\n<dt><strong>configurable<\/strong><\/dt>\n<dd>\u5f53\u4e14\u4ec5\u5f53\u6307\u5b9a\u5bf9\u8c61\u7684\u5c5e\u6027\u63cf\u8ff0\u53ef\u4ee5\u88ab\u6539\u53d8\u6216\u8005\u5c5e\u6027\u53ef\u88ab\u5220\u9664\u65f6\uff0c\u4e3atrue\u3002<\/dd>\n<dt><strong>enumerable<\/strong><\/dt>\n<dd>\u5f53\u4e14\u4ec5\u5f53\u6307\u5b9a\u5bf9\u8c61\u7684\u5c5e\u6027\u53ef\u4ee5\u88ab\u679a\u4e3e\u51fa\u65f6\uff0c\u4e3a true\u3002<\/dd>\n<\/dl>\n<p>\u8fd8\u6709\u4e2a\u7c7b\u4f3c\u7684\u9759\u6001\u65b9\u6cd5\u540d\u4e3a Reflect.getOwnPropertyDescriptor()\uff0c\u5927\u5bb6\u6709\u5174\u8da3\u53ef\u4ee5\u8bbf\u95ee\u5bf9\u5e94\u7684<a href=\"https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Reflect\/getOwnPropertyDescriptor\" rel=\"noopener nofollow\" target=\"_blank\">MDN\u6587\u6863<\/a>\u4e86\u89e3\u4e00\u756a\u3002<\/p>\n<p><span class=\"hidden\">\/\/zxx: \u5982\u679c\u4f60\u770b\u5230\u8fd9\u6bb5\u6587\u5b57\uff0c\u8bf4\u660e\u4f60\u73b0\u5728\u8bbf\u95ee\u662f\u4f53\u9a8c\u7cdf\u7cd5\u7684\u5783\u573e\u76d7\u7248\u7f51\u7ad9\uff0c\u4f60\u53ef\u4ee5\u8bbf\u95ee\u539f\u6587\u83b7\u5f97\u5f88\u597d\u7684\u4f53\u9a8c\uff1ahttps:\/\/www.zhangxinxu.com\/wordpress\/?p=9949\uff08\u4f5c\u8005\u5f20\u946b\u65ed\uff09<\/span><\/p>\n<h3>\u56db\u3001\u4e0d\u53ea\u662f\u89e6\u53d1change\u4e8b\u4ef6<\/h3>\n<p>getOwnPropertyDescriptor() \u65b9\u6cd5\u81ea\u5b9a\u4e49value\u5c5e\u6027\u540e\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u7528\u6765\u89e6\u53d1change\u4e8b\u4ef6\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4e3b\u52a8\u89e6\u53d1\u8f93\u5165\u6846\u5143\u7d20\u7684\u6e32\u67d3\u3002<\/p>\n<p>\u4f8b\u5982LuLu UI <a href=\"https:\/\/www.zhangxinxu.com\/sp\/lulu\/mockup\/edge\/apis.color.html\" rel=\"noopener\" target=\"_blank\">Edge\u4e3b\u9898\u4e2d\u7684\u989c\u8272\u9009\u62e9\u8f93\u5165\u6846<\/a>\u652f\u6301 #RRGGBBAA \u989c\u8272\u683c\u5f0f\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202105\/2021-05-13_181457.png\" width=\"703\" height=\"156\" alt=\"RRGGBBAA\u652f\u6301\u622a\u56fe\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u539f\u751f\u7684color\u8f93\u5165\u6846\u5728\u8bbe\u7f6evalue\u5c5e\u6027\u503c\u4e3a#RRGGBB\u8272\u503c\u65f6\u4f1a\u81ea\u52a8\u53d8\u8272\uff0c\u4f46\u662f\u8fd9\u91cc\u7684color-opacity\u8f93\u5165\u6846\u662f\u81ea\u5b9a\u4e49\u7684\uff0c\u5e76\u4e0d\u8bc6\u522b#RRGGBBAA\u8272\u503c\uff0c\u6b64\u65f6\u5c31\u9700\u8981\u5bf9value\u5c5e\u6027\u8fdb\u884c\u5904\u7406\uff0c\u8d4b\u503c\u7684\u65f6\u5019\u81ea\u52a8\u6539\u53d8\u8f93\u5165\u6846\u7684UI\uff0c\u5176\u539f\u7406\u548c\u4e0a\u9762\u89e6\u53d1change\u65f6\u95f4\u7c7b\u4f3c\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u610f\uff1a<\/p>\n<pre>class Color extends HTMLInputElement {\r\n    constructor () {\r\n        super();\r\n    }\r\n    ...\r\n}\r\nconst props = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value');\r\nObject.defineProperty(Color.prototype, 'value', {\r\n    ...props,\r\n    set (value) {\r\n        let strValue = value;\r\n        <span style=\"color:green;\">\/\/ \u76ee\u524d\u7684\u989c\u8272\u503c<\/span>\r\n        let strOldValue = this.value;\r\n        <span style=\"color:green;\">\/\/ UI\u540c\u6b65\u5339\u914d<\/span>\r\n        <span style=\"color:#cd0000;\">this.uiMatch();<\/span>\r\n        <span style=\"color:green;\">\/\/ \u8d4b\u503c<\/span>\r\n        props.set.call(this, strOldValue);\r\n        <span style=\"color:green;\">\/\/ change\u4e8b\u4ef6\u89e6\u53d1<\/span>\r\n        if (strOldValue &amp;amp;&amp;amp; strValue != strOldValue) {\r\n            this.dispatchEvent(new CustomEvent('change', {\r\n                'bubbles': true\r\n            }));\r\n        }\r\n    }\r\n});<\/pre>\n<p>\u6b64\u65f6\u7ed9\u8f93\u5165\u6846\u6267\u884c <code>input.value = '#EB46467D'<\/code> \u5c31\u53ef\u4ee5\u770b\u5230UI\u53d8\u5316\u4e86\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202105\/2021-05-13_183827.png\" width=\"258\" height=\"73\" alt=\"\u989c\u8272\u53d8\u5316\u4e86\u793a\u610f\" class=\"alignnone size-medium\" \/><\/p>\n<p><span class=\"s\">\/\/ zxx: \u5b9e\u9645\u4e0a\u4e0a\u9762\u8272\u503c\u4e0d\u5bf9\uff0c\u548c\u8bbe\u8ba1\u4e0d\u7b26\uff0c\u5de6\u4e0a50%\u5e94\u8be5\u662f\u7eaf\u8272\uff0c\u800c\u4e0d\u662f\u548c\u9ed1\u8272\u7684\u6df7\u5408\u8272\uff0c\u6211\u8fd9\u51e0\u5929\u62bd\u7a7a\u5904\u7406\u4e0b\u3002<\/span><\/p>\n<h3>\u4e94\u3001\u7ed3\u8bed<\/h3>\n<p>\u9664\u4e86Object.defineProperty\u65b9\u6cd5\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528Proxy\u5bf9\u8c61\u5b9e\u73b0\u7c7b\u4f3c\u7684\u6548\u679c\uff0c\u8fd9\u4e2a\u4e4b\u540e\u6709\u673a\u4f1a\u518d\u4ecb\u7ecd\u3002<\/p>\n<p>\u4eca\u5929\u53bb\u533b\u9662\u62d4\u4e86\u4e24\u9897\u667a\u9f7f\uff0c\u534a\u5f20\u8138\u90fd\u662f\u9ebb\u7684\uff0c\u73b0\u5728\u62d4\u7259\u4f4d\u7f6e\u8fd8\u75bc\uff0c\u75bc\u5230\u988c\u9762\u9aa8\u90fd\u75bc\uff0c\u597d\u60e8\u3002<\/p>\n<p>\u8fd9\u56de\u5927\u5bb6\u8981\u771f\u7684\u5fc3\u75bc\u54e5\u54e5\u4e86\uff0c\u6c42\u5206\u4eab\u6c42\u8f6c\u53d1\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202105\/2021-05-13_200632.jpg\" width=\"180\" height=\"297\" alt=\"\u5fc3\u75bc\u54e5\u54e5\" class=\"alignnone size-medium\" \/><\/p>\n<p>\u672c\u6587\u4e3a\u539f\u521b\u6587\u7ae0\uff0c\u6b22\u8fce\u5206\u4eab\uff0c\u52ff\u5168\u6587\u8f6c\u8f7d\uff0c\u5982\u679c\u5b9e\u5728\u559c\u6b22\uff0c\u53ef\u6536\u85cf\uff0c\u6c38\u4e0d\u8fc7\u671f\uff0c\u4e14\u4f1a\u53ca\u65f6\u66f4\u65b0\u77e5\u8bc6\u70b9\u53ca\u4fee\u6b63\u9519\u8bef\uff0c\u9605\u8bfb\u4f53\u9a8c\u4e5f\u66f4\u597d\u3002<br \/>\n\u672c\u6587\u5730\u5740\uff1a<a href=\"https:\/\/www.zhangxinxu.com\/wordpress\/?p=9949\">https:\/\/www.zhangxinxu.com\/wordpress\/?p=9949<\/a><\/p>\n<p>\uff08\u672c\u7bc7\u5b8c\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/image.zhangxinxu.com\/image\/blog\/202105\/value-change-cover.png\" width=\"160\" height=\"100\" alt=\"\u706b\u7bad\u98de\u789f\" class=\"alignright size-medium\" loading=\"lazy\" \/><\/p>\n<p>HTML\u8868\u5355\u8f93\u5165\u6846\u5143\u7d20\u76f4\u63a5\u4f7f\u7528value\u5c5e\u6027\u8d4b\u503c\u662f\u4e0d\u4f1a\u89e6\u53d1change\u4e8b\u4ef6\u7684\uff0c\u672c\u6587\u63d0\u4f9b\u4e00\u79cd\u5b9e\u73b0\u65b9\u6cd5\uff0c\u4fdd\u8bc1\u539f\u751f\u7684value\u8d4b\u503c\u7279\u6027\u540c\u65f6\u53ef\u4ee5\u89e6\u53d1change\u4e8b\u4ef6\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[180],"tags":[1650,1474,708,1755,1756,1757],"_links":{"self":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9949"}],"collection":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/comments?post=9949"}],"version-history":[{"count":0,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/posts\/9949\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/media?parent=9949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/categories?post=9949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhangxinxu.com\/wordpress\/wp-json\/wp\/v2\/tags?post=9949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}