vue自定义指令 focus获取焦点

    <input type="text" v-focus> 

 1.// 全局自定义指令  代码要放在new Vue的上面

    // 注册一个全局自定义指令 `v-focus`

    //  使用 Vue.directive() 定义全局的指令  v-focus

    //  其中参数1 :指令的名称,注意,在定义的时候,指令的名称前面,不需要加 v- 前缀

    //  但是:在调用的时候,必须在指令名称前 加上 -v 前缀来进行调用

    //  参数2:是一个对象,这个对象身上,有一些指令相关的函数,这些函数可以再特定的阶段,执行相关的操作

     Vue.directive('focus', {

      // 当被绑定的元素插入到 DOM 中时……

      inserted: function (el) {

        // 聚焦元素

        el.focus()

      }

    })

 <div v-fontweight='900' v-color="'pink'">{{msg}}</div>

    Vue.directive('color', {

      bind: function (el, binding) {

        el.style.color = binding.value

      }

    })

2、局部自定义指令

directives: {

        'fontweight': {

          bind: function (el, binding) {

            el.style.fontWeight = binding.value

          }

        },

3、简写方式

        'fontsize':  function (el, binding) {

            el.style.fontSize = parseInt(binding.value) + "px"

          }

      }

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div id='app'>
    <!-- 参数一定要加引号 -->
    <div v-fontweight='900' v-color="'pink'">{{msg}}</div> 
    <input type="text" v-focus>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <script>
    // 全局自定义指令
    // 注册一个全局自定义指令 `v-focus`
    //  使用 Vue.directive() 定义全局的指令  v-focus
    //  其中参数1 :指令的名称,注意,在定义的时候,指令的名称前面,不需要加 v- 前缀
    //  但是:在调用的时候,必须在指令名称前 加上 -v 前缀来进行调用
    //  参数2:是一个对象,这个对象身上,有一些指令相关的函数,这些函数可以再特定的阶段,执行相关的操作
    Vue.directive('focus', {
      // 当被绑定的元素插入到 DOM 中时……
      inserted: function (el) {
        // 聚焦元素
        el.focus()
      }
    })

    Vue.directive('color', {
      bind: function (el, binding) {
        el.style.color = binding.value
      }
    })
    var vm = new Vue({
      el: '#app',
      data: {
        msg: 'dsadsada萨达啊大萨达撒'
      },
      // 自定义私有指令
      directives: {
        'fontweight': {
          bind: function (el, binding) {
            el.style.fontWeight = binding.value
          }
        }
      }
    })
  </script>
</body>

</html>

<!-- 过滤可被用作一些常见的文本格式化,过滤器可以用在两个地方 mustache插值、v-bind表达式(其他地方一律不能用) -->
<!-- 优先调用私有过滤器 -->
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Vue创建一个自定义指令,让输入框获取焦点,你需要在Vue实例的directives属性定义一个名为"focus"的指令,并在inserted方法使用原生的focus()方法将焦点设置为当前元素。具体代码如下: ``` export default { directives: { focus: { inserted(el) { el.focus(); } } } } ``` 然后,在需要获取焦点的输入框上使用v-focus指令即可。在App.vue的模板,你可以将v-focus指令添加到<input type="text">元素上,如下所示: ``` <template> <div> <input type="text" v-focus> </div> </template> ``` 这样,当组件渲染完成后,该输入框会自动获取焦点。请注意,这个自定义指令只在当前组件的作用域内有效。如果你希望在全局范围内使用这个指令,你需要在全局注册它。可以使用Vue.directive方法实现全局注册,如下所示: ``` Vue.directive("focus", { inserted(el) { el.focus(); } }); ``` 这样,无论在哪个组件,你都可以在输入框上使用v-focus指令,以获取焦点。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Vue 自定义指令 自动获取焦点](https://blog.csdn.net/laoli360/article/details/120555899)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值