最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - VueJS trigger an event when a field is in focus - Stack Overflow

programmeradmin2浏览0评论

I have a password change page with VueJS, and I want to view the password policy only when the user clicks into the new password field. The problem is that I can't find if the page is in focus..

<b-row>
  <b-col>
    <b-form-group id="newPasswrd" label="New Password" :state="statePassword">
      <b-input-group>
        <b-form-input id="newPassword" v-model="passwords.newPassword" ref="newPassword" placeholder="Passwort" type="password" maxlength="60" />
        <p class="password-description"  >Must be at least 8 characters and contain at least two of the following: Upper case, Lower case, special characters or numbers</p>            
      </b-input-group>
    </b-form-group>
  </b-col>
</b-row>

and this is what I have in the script part of the page for testing:

      if (this.$refs.newPassword.focus() == true) console.log("focus");

My plan is to ultimately put this line in a puted and attach a bool value to it to view/hide the text below the field depending on whether if it is in focus. What happens is that I get nothing in the console when I trigger the method where this condition is written, but the focus es to the field instead, which is not what I want. what should I do to get a bool value if the field is in focus?

I have a password change page with VueJS, and I want to view the password policy only when the user clicks into the new password field. The problem is that I can't find if the page is in focus..

<b-row>
  <b-col>
    <b-form-group id="newPasswrd" label="New Password" :state="statePassword">
      <b-input-group>
        <b-form-input id="newPassword" v-model="passwords.newPassword" ref="newPassword" placeholder="Passwort" type="password" maxlength="60" />
        <p class="password-description"  >Must be at least 8 characters and contain at least two of the following: Upper case, Lower case, special characters or numbers</p>            
      </b-input-group>
    </b-form-group>
  </b-col>
</b-row>

and this is what I have in the script part of the page for testing:

      if (this.$refs.newPassword.focus() == true) console.log("focus");

My plan is to ultimately put this line in a puted and attach a bool value to it to view/hide the text below the field depending on whether if it is in focus. What happens is that I get nothing in the console when I trigger the method where this condition is written, but the focus es to the field instead, which is not what I want. what should I do to get a bool value if the field is in focus?

Share Improve this question asked Sep 3, 2018 at 9:36 j0zeftj0zeft 6592 gold badges14 silver badges32 bronze badges 2
  • 1 How about an onfocus event? w3schools./jsref/event_onfocus.asp – Roberto Zvjerković Commented Sep 3, 2018 at 9:38
  • 2 Try with v-on:focus. – Ankit Agarwal Commented Sep 3, 2018 at 9:39
Add a ment  | 

3 Answers 3

Reset to default 8

Bind using v-on with the native modifier:

v-on:focus.native="onFocus"

From a Core Member

Binding Native Events to Components

Have you tried this with css only?

If you don't have to support IE11/Edge:

b-input-group:focus-within>p {
    display: block;
}

If you have to support IE11/Edge:

input:focus + p {
    display: block;
}

I would also remend adding classes instead of the barebone elements.

You can create custom directive

Sample from official vuejs docs

Vue.directive('focus', {
  // When the bound element is inserted into the DOM...
  inserted: function (el) {
    // Focus the element
    el.focus()
  }
})
发布评论

评论列表(0)

  1. 暂无评论