Implementation of element input box automatically getting focus

Implementation of element input box automatically getting focus

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:
  • Solution for Vue form input box not supporting focus and blur events
  • Vue implements the fuzzy query method of Input input box
  • The input box in Vue does not focus after clicking

<<:  Implementation of nginx virtual host settings based on domain name, port, and different IP

>>:  Summary of HTML knowledge points for the front end (recommended)

Recommend

Various problems encountered by novices when installing mysql into docker

Preface Recently, my computer often takes a long ...

HTML tbody usage

Structured Table (IExplore Only) 1) Group by rows ...

Element Plus implements Affix

Table of contents 1. Component Introduction 2. So...

Implementation steps of mysql master-slave replication

Table of contents mysql master-slave replication ...

MySQL loop inserts tens of millions of data

1. Create a test table CREATE TABLE `mysql_genara...

MySQL encoding utf8 and utf8mb4 utf8mb4_unicode_ci and utf8mb4_general_ci

Reference: MySQL character set summary utf8mb4 ha...

CentOS 6.4 MySQL 5.7.18 installation and configuration method graphic tutorial

The specific steps of installing mysql5.7.18 unde...

Mobile front-end adaptation solution (summary)

I searched online and found that many interviews ...

Use and optimization of MySQL COUNT function

Table of contents What does the COUNT function do...

Detailed example of deploying Nginx+Apache dynamic and static separation

Introduction to Nginx dynamic and static separati...

How to configure multiple projects with the same domain name in Nginx

There are two ways to configure multiple projects...

Implement group by based on MySQL to get the latest data of each group

Preface: The group by function retrieves the firs...

How to create a my.ini file in the MySQL 5.7.19 installation directory

In the previous article, I introduced the detaile...