{"id":53071,"date":"2024-12-03T08:38:24","date_gmt":"2024-12-03T00:38:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53071\/"},"modified":"2024-12-03T08:38:24","modified_gmt":"2024-12-03T00:38:24","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-3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53071\/","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><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5feb\u901f\u4fbf\u6377\u5730\u4e3a Input \u65bd\u52a0 focus \u65b9\u6cd5\u5e76\u8bbe\u7f6e\u5149\u6807\u4f4d\u7f6e\uff1f <\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5feb\u901f\u4fbf\u6377\u5730\u4e3a Input \u65bd\u52a0 focus \u65b9\u6cd5\u5e76\u8bbe\u7f6e\u5149\u6807\u4f4d\u7f6e\uff1f \u300b<\/span>\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241126\/1732597973674558d5c6b6e.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 main.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>\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><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>\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>\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>\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\u300a\u5982\u4f55\u5feb\u901f\u4fbf\u6377\u5730\u4e3a Input \u65bd\u52a0 focus \u65b9\u6cd5\u5e76\u8bbe\u7f6e\u5149\u6807\u4f4d\u7f6e\uff1f \u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   Win10\u5f00\u673a\u5361\u5728\u9009\u62e9\u952e\u76d8\u5e03\u5c40\u95ee\u9898\u600e\u4e48\u529e\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5feb\u901f\u4fbf\u6377\u5730\u4e3a Input \u65bd\u52a0 focus \u65b9\u6cd5\u5e76\u8bbe\u7f6e\u5149\u6807\u4f4d\u7f6e\uff1f \u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587\u300a\u5982\u4f55\u5feb\u901f\u4fbf\u6377\u5730\u4e3a Input \u65bd\u52a0 focus \u65b9\u6cd5\u5e76\u8bbe\u7f6e\u5149\u6807\u4f4d\u7f6e\uff1f \u300b\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a\u6587\u7ae0\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~ \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 main.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; }); }); } }); \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; [&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-53071","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53071","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=53071"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53071\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}