10.Vue.js : 自动获取焦点dom版本-ref

mounted

mounted 钩子函数很特殊,他表示页面一加载进来就执行函数里面的内容(和window.onload类似);

注意:mounted钩子函数的名字不能随便取,不能写在methods属性中

ref

ref 属性表示对dom的引用,它的值可以随便取,但是不能重复
<input type="text" v-model="newId" id='inputId' ref='inputRefId'> 

mounted() {
            // document.getElementById('inputId').focus()原生js方式实现的
            //通过this.$refs.ref的值--来获取dom
              this.$refs.inputRefId.focus()
      }

自定义获取焦点自定义指令版本

<input type="text" v-model="newId" id='inputId' ref='inputRefId' v-myfocus> 

自定义指令通过Vue.directive()创建,它包含两个参数,一个是自定义指令的名字,可以随便取;
但是全部小写,另一个是一个对象,表示自定义指令的配置项。

//这里实际上创建的是v-myfocus指令
        Vue.directive('myfocus', {
            //inserted钩子函数,它表示自定义指令插入到标签中的时候就执行
            //inserter钩子函数有两个参数(el,bingding) el表示使用自定义指令的元素,binding表示自 
             定义指令的信息
            inserted(el, binding) {
                el.focus();
            }
        })
<input type="text" v-model="newId" id='inputId' ref='inputRefId' v-myfocus v-mycolor='color'> 

 //创建是的v-mycolor='color'指令
        Vue.directive('mycolor', {
            inserted(el, binding) {
                //binding.value可以获取传入自定义指令中的属性的值
                el.style.color = binding.value
            }
        })
<!DOCTYPE html>
<html lang="en">

<head>
    <title>列表展示删除添加</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="./vue.js"></script>
    <style>
        #app {
            width: 600px;
            margin: 20px auto;
        }
        
        .dv1,
        .dv2 {
            border: 1px solid black;
            margin-bottom: 10px;
            padding: 5px;
        }
        
        table {
            width: 600px;
            border-right: 1px solid black;
            border-top: 1px solid black;
        }
        
        .tb th {
            width: 600px;
            background-color: blue;
            color: white;
            border-bottom: 1px solid #000;
        }
        
        .tb th,
        .tb td {
            border-left: 1px solid black;
            border-bottom: 1px solid #000;
            text-align: center;
            padding: 5px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="dv1">
            编号:
            <input type="text" v-model="newId" id='inputId' ref='inputRefId' v-myfocus v-mycolor='color'> 品牌名称:
            <input type="text" v-model="newName">
            <input type="button" value="添加" @click='addData'>
        </div>
        <div class="dv2">
            品牌名称:
            <input type="text" placeholder="请输入搜索条件">

        </div>
        <div class="tb">
            <table cellpadding="0" cellspacing="0">
                <tr>
                    <th>编号</th>
                    <th>品牌名称</th>
                    <th>创立时间</th>
                    <th>操作</th>
                </tr>
                <tr v-for='(item,index) in list' :key='index'>
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.ctime}}</td>
                    <td><a href="#" @click='deleteData(index)'>删除</a></td>
                </tr>
                <tr v-if='list.length===0'>
                    <td colspan='4'>没有品牌数据</td>
                </tr>
            </table>
        </div>
    </div>
    <script>
        //这里实际上创建的是v-myfocus指令
        Vue.directive('myfocus', {
            //inserted钩子函数,它表示自定义指令插入到标签中的时候就执行
            //inserter钩子函数有两个参数(el,bingding) el表示使用自定义指令的元素,binding表示自定义指令的信息
            inserted(el, binding) {
                el.focus();
            }
        })
        //创建是的v-mycolor='color'指令
        Vue.directive('mycolor', {
            inserted(el, binding) {
                //binding.value可以获取传入自定义指令中的属性的值
                el.style.color = binding.value
            }
        })
        var vm = new Vue({
            el: '#app',
            data: {
                newId: '',
                newName: '',
                color: 'red',
                list: [{
                    id: 1,
                    name: '宝马',
                    ctime: new Date()
                }, {
                    id: 2,
                    name: '奔驰',
                    ctime: new Date()
                }, {
                    id: 3,
                    name: '大众',
                    ctime: new Date()
                }, {
                    id: 4,
                    name: '海马',
                    ctime: new Date()
                }, ]
            },
            // mounted() {
            // document.getElementById('inputId').focus()原生js方式实现的
            //通过this.$refs.ref的值--来获取dom
            // this.$refs.inputRefId.focus()
            // },
            methods: {
                deleteData(idx) {
                    this.list.splice(idx, 1)
                },
                addData() {
                    this.list.push({
                        id: this.newId,
                        name: this.newName,
                        ctime: new Date()
                    })
                }
            }
        })
    </script>
</body>

</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

屋顶上的小喵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值