{"id":27285,"date":"2024-11-24T09:19:46","date_gmt":"2024-11-24T01:19:46","guid":{"rendered":"https:\/\/fwq.ai\/blog\/27285\/"},"modified":"2024-11-24T09:19:46","modified_gmt":"2024-11-24T01:19:46","slug":"%e5%a6%82%e4%bd%95%e5%bf%ab%e9%80%9f%e4%be%bf%e6%8d%b7%e5%9c%b0%e4%b8%ba-input-%e6%96%bd%e5%8a%a0-focus-%e6%96%b9%e6%b3%95%e5%b9%b6%e8%ae%be%e7%bd%ae%e5%85%89%e6%a0%87%e4%bd%8d%e7%bd%ae%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/27285\/","title":{"rendered":"\u5982\u4f55\u5feb\u901f\u4fbf\u6377\u5730\u4e3a Input \u65bd\u52a0 focus \u65b9\u6cd5\u5e76\u8bbe\u7f6e\u5149\u6807\u4f4d\u7f6e\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173122427361532.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5feb\u901f\u4fbf\u6377\u5730\u4e3a Input \u65bd\u52a0 focus \u65b9\u6cd5\u5e76\u8bbe\u7f6e\u5149\u6807\u4f4d\u7f6e\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5feb\u901f\u4fbf\u6377\u5730\u4e3a Input \u65bd\u52a0 focus \u65b9\u6cd5\u5e76\u8bbe\u7f6e\u5149\u6807\u4f4d\u7f6e\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5feb\u901f\u4fbf\u6377\u5730\u4e3a input \u65bd\u52a0 focus \u65b9\u6cd5<\/strong><\/p>\n<p>\u5bf9\u4e8e\u9700\u8981\u5feb\u901f\u5728\u8f93\u5165\u6846\u83b7\u5f97\u7126\u70b9\u5e76\u8bbe\u7f6e\u5149\u6807\u4f4d\u7f6e\u7684\u573a\u666f\uff0c\u73b0\u6709\u7684\u4ee3\u7801\u9700\u8981\u5728\u6bcf\u4e2a\u7ec4\u4ef6\u4e2d\u8fdb\u884c\u91cd\u590d\u7684\u4fee\u6539\uff0c\u8fd9\u65e2\u9ebb\u70e6\u53c8\u8017\u65f6\u3002<\/p>\n<p><strong>\u5168\u5c40\u81ea\u5b9a\u4e49\u6307\u4ee4<\/strong><\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u501f\u52a9 Vue.js \u7684\u81ea\u5b9a\u4e49\u6307\u4ee4\u529f\u80fd\u3002\u5728 mn.js \u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>Vue.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>\u4e4b\u540e\uff0c\u5728\u9700\u8981\u4f7f\u7528\u8be5\u529f\u80fd\u7684\u7ec4\u4ef6\u4e2d\uff0c\u53ea\u9700\u8981\u5728\u8f93\u5165\u6846\u4e0a\u6dfb\u52a0 v-focus-right \u6307\u4ee4\u5373\u53ef\uff1a<\/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>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 Vue.js \u63d2\u4ef6\u3002\u5728 focusPlugin.js \u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>const 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};<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728 main.js \u6587\u4ef6\u4e2d\u5bfc\u5165\u8be5\u63d2\u4ef6\u5e76\u4f7f\u7528\uff1a<\/p>\n<pre>import FocusRightPlugin from '.\/focusPlugin';\n\nVue.use(FocusRightPlugin);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u6837\uff0c\u5728\u7ec4\u4ef6\u4e2d\u53ef\u4ee5\u4f7f\u7528 $inputFocusRight \u65b9\u6cd5\uff1a<\/p>\n<pre>&lt;input @focus=\"$inputFocusRight\"&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u4e24\u79cd\u65b9\u6cd5\u90fd\u53ef\u4ee5\u5feb\u901f\u3001\u8f7b\u677e\u5730\u4e3a\u8f93\u5165\u6846\u65bd\u52a0 focus \u65b9\u6cd5\uff0c\u65e0\u9700\u5728\u6bcf\u4e2a\u7ec4\u4ef6\u4e2d\u91cd\u590d\u5197\u957f\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5feb\u901f\u4fbf\u6377\u5730\u4e3a Input \u65bd\u52a0 focus \u65b9\u6cd5\u5e76\u8bbe\u7f6e\u5149\u6807\u4f4d\u7f6e\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>\u5feb\u901f\u4fbf\u6377\u5730\u4e3a input \u65bd\u52a0 focus \u65b9\u6cd5 \u5bf9\u4e8e\u9700\u8981\u5feb\u901f\u5728\u8f93\u5165\u6846\u83b7\u5f97\u7126\u70b9\u5e76\u8bbe\u7f6e\u5149\u6807\u4f4d\u7f6e\u7684\u573a\u666f\uff0c\u73b0\u6709\u7684\u4ee3\u7801\u9700\u8981\u5728\u6bcf\u4e2a\u7ec4\u4ef6\u4e2d\u8fdb\u884c\u91cd\u590d\u7684\u4fee\u6539\uff0c\u8fd9\u65e2\u9ebb\u70e6\u53c8\u8017\u65f6\u3002 \u5168\u5c40\u81ea\u5b9a\u4e49\u6307\u4ee4 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u501f\u52a9 Vue.js \u7684\u81ea\u5b9a\u4e49\u6307\u4ee4\u529f\u80fd\u3002\u5728 mn.js \u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a 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 \u4e4b\u540e\uff0c\u5728\u9700\u8981\u4f7f\u7528\u8be5\u529f\u80fd\u7684\u7ec4\u4ef6\u4e2d\uff0c\u53ea\u9700\u8981\u5728\u8f93\u5165\u6846\u4e0a\u6dfb\u52a0 v-focus-right \u6307\u4ee4\u5373\u53ef\uff1a &lt;input v-focus-right&gt; \u767b\u5f55\u540e\u590d\u5236 \u63d2\u4ef6 \u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 Vue.js \u63d2\u4ef6\u3002\u5728 focusPlugin.js \u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a const FocusRightPlugin = [&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-27285","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27285","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=27285"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27285\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=27285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=27285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=27285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}