When making a form in a recent project, I need to automatically scroll to the comment box and make the comment box automatically focused, which requires manually triggering the focus state of the input box. However, element does not support the autofocus attribute, so you can only get the focus effect through native js effects. document.getElementById("input").focus(); Or you can use the ref attribute of vue to achieve the focusing effect: The principle is actually very simple. Element already provides a focus method, but the documentation does not specify how to call it. The following is to add a ref attribute to the el-input tag, and then call the method directly where needed. <el-input v-model="input" placeholder="Please enter content" ref="input"></el-input> this.$nextTick(() => { this.$refs.input.focus() }) Note: A page can only have one focus effect Last, vue also supports custom instructions When the page loads, the element will receive focus (note: autofocus does not work on mobile Safari). In fact, as long as you haven't clicked anything since opening the page, the input box should still be in focus. Now let's implement this functionality using instructions: // Register a global custom directive `v-focus` Vue.directive('focus', { // When the bound element is inserted into the DOM... inserted: function (el) { // Focus element el.focus() //element-ui el.children[0].focus() // If the element changes, such as show or parent element changes, you can add a delay or judge setTimeout(_ => { el.children[0].focus() }) } }) Reference: vue custom directive https://cn.vuejs.org/v2/guide/custom-directive.html This is the end of this article about how to automatically get the focus of the element input box. For more information about how to automatically get the focus of the element input box, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Implementation of nginx virtual host settings based on domain name, port, and different IP
>>: Summary of HTML knowledge points for the front end (recommended)
Preface Recently, my computer often takes a long ...
Table of contents 1. Page Layout 2. Image upload ...
Structured Table (IExplore Only) 1) Group by rows ...
Table of contents 1. Component Introduction 2. So...
Table of contents mysql master-slave replication ...
1. Create a test table CREATE TABLE `mysql_genara...
Reference: MySQL character set summary utf8mb4 ha...
The specific steps of installing mysql5.7.18 unde...
I searched online and found that many interviews ...
Table of contents What does the COUNT function do...
It is not possible to use width and height directl...
Introduction to Nginx dynamic and static separati...
There are two ways to configure multiple projects...
Preface: The group by function retrieves the firs...
In the previous article, I introduced the detaile...