【问题标题】:Vue.js on focus textbox on load加载时焦点文本框上的 Vue.js
【发布时间】:2016-12-23 08:00:26
【问题描述】:

如何使用 VueJS 专注于负载?

如果使用 jquery,我应该做的就是:

$(document).ready(function() {
   $('#username').focus();
});

有没有vue-way?

【问题讨论】:

标签: vue.js vuejs2


【解决方案1】:

您可以为此创建自定义指令:

// Register a global custom directive called v-focus
Vue.directive('focus', {
  // When the bound element is inserted into the DOM...
  inserted: function (el) {
    // Focus the element
    el.focus()
  }
})

然后像这样使用它:

<input v-focus>

来自文档的完整示例:https://vuejs.org/v2/guide/custom-directive.html

指令文档:https://vuejs.org/v2/api/#Vue-directive

【讨论】:

    猜你喜欢
    • 2017-09-20
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多