{"id":27344,"date":"2024-11-24T15:35:23","date_gmt":"2024-11-24T07:35:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/27344\/"},"modified":"2024-11-24T15:35:23","modified_gmt":"2024-11-24T07:35:23","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-vue-js-%e4%b8%ad%e4%be%bf%e6%8d%b7%e5%9c%b0%e5%b0%86%e8%be%93%e5%85%a5%e6%a1%86%e7%84%a6%e7%82%b9%e7%bd%ae%e4%ba%8e%e5%8f%b3%e4%be%a7%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/27344\/","title":{"rendered":"\u5982\u4f55\u5728 Vue.js \u4e2d\u4fbf\u6377\u5730\u5c06\u8f93\u5165\u6846\u7126\u70b9\u7f6e\u4e8e\u53f3\u4fa7\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173123230836043.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728 Vue.js \u4e2d\u4fbf\u6377\u5730\u5c06\u8f93\u5165\u6846\u7126\u70b9\u7f6e\u4e8e\u53f3\u4fa7\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728 Vue.js \u4e2d\u4fbf\u6377\u5730\u5c06\u8f93\u5165\u6846\u7126\u70b9\u7f6e\u4e8e\u53f3\u4fa7\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4fbf\u6377\u7ed9 input \u65bd\u52a0 focus \u65b9\u6cd5<\/strong><\/p>\n<p>\u5728 Vue.js \u4e2d\uff0c\u9700\u8981\u5728\u8f93\u5165\u6846\u83b7\u53d6\u7126\u70b9\u65f6\u5c06\u5149\u6807\u7f6e\u4e8e\u53f3\u4fa7\u3002\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5feb\u901f\u4fbf\u5229\u5730\u89e3\u51b3\u6b64\u95ee\u9898\uff1a<\/p>\n<p><strong>\u81ea\u5b9a\u4e49\u6307\u4ee4<\/strong><\/p>\n<pre>\/\/ main.js\nVue.directive('focus-right', {\n  inserted: function (el) {\n    el.addEventListener('focus', function () {\n      const length = el.value.length;\n      setTimeout(() =&gt; {\n        el.selectionStart = length;\n        el.selectionEnd = length;\n      });\n    });\n  }\n});<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7ec4\u4ef6\u4e2d\u4f7f\u7528<\/strong><\/p>\n<pre>&lt;input v-focus-right&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u63d2\u4ef6<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>\/\/ focusPlugin.js\nconst FocusRightPlugin = {\n  install(Vue) {\n    Vue.prototype.$inputFocusRight = function (e) {\n      const input = e.target;\n      const length = input.value.length;\n      setTimeout(() =&gt; {\n        input.selectionStart = length;\n        input.selectionEnd = length;\n      });\n    };\n  }\n};\n\n\/\/ main.js\nimport FocusRightPlugin from '.\/focusPlugin';\n\nVue.use(FocusRightPlugin);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u7ec4\u4ef6\u4e2d\u4f7f\u7528<\/strong><\/p>\n<pre>&lt;input @focus=\"$inputFocusRight\"&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728 Vue.js \u4e2d\u4fbf\u6377\u5730\u5c06\u8f93\u5165\u6846\u7126\u70b9\u7f6e\u4e8e\u53f3\u4fa7\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4fbf\u6377\u7ed9 input \u65bd\u52a0 focus \u65b9\u6cd5 \u5728 Vue.js \u4e2d\uff0c\u9700\u8981\u5728\u8f93\u5165\u6846\u83b7\u53d6\u7126\u70b9\u65f6\u5c06\u5149\u6807\u7f6e\u4e8e\u53f3\u4fa7\u3002\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5feb\u901f\u4fbf\u5229\u5730\u89e3\u51b3\u6b64\u95ee\u9898\uff1a \u81ea\u5b9a\u4e49\u6307\u4ee4 \/\/ main.js Vue.directive(&#8216;focus-right&#8217;, { inserted: function (el) { el.addEventListener(&#8216;focus&#8217;, function () { const length = el.value.length; setTimeout(() =&gt; { el.selectionStart = length; el.selectionEnd = length; }); }); } }); \u767b\u5f55\u540e\u590d\u5236 \u7ec4\u4ef6\u4e2d\u4f7f\u7528 &lt;input v-focus-right&gt; \u767b\u5f55\u540e\u590d\u5236 \u63d2\u4ef6 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \/\/ focusPlugin.js const FocusRightPlugin = { install(Vue) { Vue.prototype.$inputFocusRight = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-27344","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=27344"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27344\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=27344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=27344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=27344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}