{"id":27458,"date":"2024-11-24T13:10:42","date_gmt":"2024-11-24T05:10:42","guid":{"rendered":"https:\/\/fwq.ai\/blog\/27458\/"},"modified":"2024-11-24T13:10:42","modified_gmt":"2024-11-24T05:10:42","slug":"vue-%e9%a1%b9%e7%9b%ae%e4%b8%ad%e5%a6%82%e4%bd%95%e4%be%bf%e6%8d%b7%e5%9c%b0%e7%bb%99-input-%e5%85%83%e7%b4%a0%e6%b7%bb%e5%8a%a0-focus-%e6%96%b9%e6%b3%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/27458\/","title":{"rendered":"Vue \u9879\u76ee\u4e2d\u5982\u4f55\u4fbf\u6377\u5730\u7ed9 input \u5143\u7d20\u6dfb\u52a0 focus \u65b9\u6cd5\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173124758547913.jpg\" class=\"aligncenter\" title=\"Vue \u9879\u76ee\u4e2d\u5982\u4f55\u4fbf\u6377\u5730\u7ed9 input \u5143\u7d20\u6dfb\u52a0 focus \u65b9\u6cd5\uff1f\u63d2\u56fe\" alt=\"Vue \u9879\u76ee\u4e2d\u5982\u4f55\u4fbf\u6377\u5730\u7ed9 input \u5143\u7d20\u6dfb\u52a0 focus \u65b9\u6cd5\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4fbf\u6377\u7ed9input\u65bd\u52a0focus\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728Vue\u9879\u76ee\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u7ed9input\u5143\u7d20\u52a0\u4e0afocus\u65b9\u6cd5\uff0c\u4f7f\u5176\u83b7\u5f97\u7126\u70b9\u5e76\u5149\u6807\u7f6e\u4e8e\u53f3\u4fa7\u3002\u4f20\u7edf\u7684\u505a\u6cd5\u662f\u7f16\u5199\u81ea\u5b9a\u4e49\u65b9\u6cd5\u5e76\u7ed1\u5b9a\u5230focus\u4e8b\u4ef6\uff0c\u8fd9\u8f83\u4e3a\u5197\u957f\u3002<\/p>\n<p>\u4e3a\u4e86\u7b80\u5316\u8fd9\u4e00\u64cd\u4f5c\uff0c\u6709\u4ee5\u4e0b\u4e09\u79cd\u4fbf\u6377\u7684\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u5168\u5c40\u81ea\u5b9a\u4e49\u6307\u4ee4<\/strong><\/p>\n<p>\u5728mn.js\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u6307\u4ee4\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/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>\u7136\u540e\u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528\uff1a<\/p>\n<pre>&lt;input v-focus-right&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u63d2\u4ef6<\/strong><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u63d2\u4ef6\u6587\u4ef6focusPlugin.js\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>\u5728main.js\u4e2d\u5bfc\u5165\u5e76\u5b89\u88c5\u63d2\u4ef6\uff1a<\/p>\n<pre>import FocusRightPlugin from '.\/focusPlugin';\n\nVue.use(FocusRightPlugin);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528\uff1a<\/p>\n<pre>&lt;input @focus=\"$inputFocusRight\"&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u8f85\u52a9\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728\u7ec4\u4ef6\u4e2d\u521b\u5efa\u4e00\u4e2a\u8f85\u52a9\u65b9\u6cd5inputFocusRight\uff0c\u8be5\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u4e8b\u4ef6\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5305\u542b\u4e86\u4f7f\u8f93\u5165\u6846\u83b7\u5f97\u7126\u70b9\u5e76\u5149\u6807\u7f6e\u4e8e\u53f3\u4fa7\u7684\u903b\u8f91\u3002<\/p>\n<pre>methods: {\n  inputFocusRight(e) {\n    const editTask = e.srcElement;\n    const length = editTask.value.length;\n    editTask.focus();\n\n    setTimeout(() =&gt; {\n      editTask.selectionStart = length;\n      editTask.selectionEnd = length;\n    });\n  }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\u5c06\u6b64\u65b9\u6cd5\u7ed1\u5b9a\u5230input\u5143\u7d20\u7684focus\u4e8b\u4ef6\uff1a<\/p>\n<pre>&lt;input @focus=\"inputFocusRight\"&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fVue \u9879\u76ee\u4e2d\u5982\u4f55\u4fbf\u6377\u5730\u7ed9 input \u5143\u7d20\u6dfb\u52a0 focus \u65b9\u6cd5\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\u7ed9input\u65bd\u52a0focus\u65b9\u6cd5 \u5728Vue\u9879\u76ee\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u7ed9input\u5143\u7d20\u52a0\u4e0afocus\u65b9\u6cd5\uff0c\u4f7f\u5176\u83b7\u5f97\u7126\u70b9\u5e76\u5149\u6807\u7f6e\u4e8e\u53f3\u4fa7\u3002\u4f20\u7edf\u7684\u505a\u6cd5\u662f\u7f16\u5199\u81ea\u5b9a\u4e49\u65b9\u6cd5\u5e76\u7ed1\u5b9a\u5230focus\u4e8b\u4ef6\uff0c\u8fd9\u8f83\u4e3a\u5197\u957f\u3002 \u4e3a\u4e86\u7b80\u5316\u8fd9\u4e00\u64cd\u4f5c\uff0c\u6709\u4ee5\u4e0b\u4e09\u79cd\u4fbf\u6377\u7684\u65b9\u6cd5\uff1a 1. \u5168\u5c40\u81ea\u5b9a\u4e49\u6307\u4ee4 \u5728mn.js\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u6307\u4ee4\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 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 \u7136\u540e\u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528\uff1a &lt;input v-focus-right&gt; \u767b\u5f55\u540e\u590d\u5236 2. \u63d2\u4ef6 \u521b\u5efa\u4e00\u4e2a\u63d2\u4ef6\u6587\u4ef6focusPlugin.js\uff1a const FocusRightPlugin = { install(Vue) { Vue.prototype.$inputFocusRight = function (e) { const input [&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-27458","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27458","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=27458"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/27458\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=27458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=27458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=27458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}