js模拟v-model指令

           v-model原理是通过使用v-model指令在元素的输入元素上创建双向数据绑定,它会根据控件类型自动选取正确的方法来更新元素。v-model 本质上不过是语法糖。它负责监听用户的输入事件以更新数据。 当input输入框值改变/复选框值改变时,text/checkbox值也会同时改变,它负责监听用户的输入事件以更新数据。

一、先写两个标签和对应的data数据。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="text">
    <p></p>
</body>
<script>
    let data ={
        msg:'vue的双向数据绑定'
    }
</script>
</html>

二、通过获取对应的标签,先初始化他的值。

    
    const inpBT = document.querySelector('input');
    const pBT = document.querySelector('p')
    inpBT.value = data.msg
    pBT.innerHTML = data.msg

三、使用Object.defindProperty中的方法。

        Object.defindProperty中有三个参数。即obj:对象, prop:key, descriptor:属性和方法。

Object.defineProperty(data,'msg',{
        // configurable  表示可配置的,默认值为false
        // enumerable    表示可枚举的
        // value         表示属性对应的值,默认undefined
        // writable      能被赋值运算符更改成功 
        // 当获取该属性时,执行get函数,属性值就是get函数的返回值
        get:function(){
        },
        // set方法默认会将设置的值作为set函数的第一个参数传递进去
        set:function(newValue){
            // 通过这个方法讲data与标签进行复制更新
            inpBT.value = newValue;
            pBT.innerHTML = newValue;
        }
    })

四、通过addEventListener监听事件来监听input框的变化

inpBT.addEventListener('input',function(){
        data.msg=this.value
    })

效果如下

 完整代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="text">
    <p></p>
</body>
<script>
    let data ={
        msg:'vue的双向数据绑定'
    }
    const inpBT = document.querySelector('input');
    const pBT = document.querySelector('p');
    inpBT.value = data.msg;
    pBT.innerHTML = data.msg;

    Object.defineProperty(data,'msg',{
        get:function(){
        },
        set:function(newValue){

            inpBT.value = newValue;
            pBT.innerHTML = newValue;
        }
    })

    inpBT.addEventListener('input',function(){
        data.msg=this.value
    })
</script>
</html>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue-SimpleKeyboard 是一个基于 Vue 的虚拟键盘组件,可以用于模拟键盘输入。它支持多种语言和布局,可以自定义键盘样式,支持键盘事件监听和回调函数,支持移动端和桌面端,提供了超丝滑的键盘输入体验。 使用 Vue-SimpleKeyboard 非常简单,只需要在 Vue 组件中引入组件,然后在模板中使用即可。例如: ```html <template> <div> <vue-simple-keyboard layout="default" v-model="inputText" /> <p>输入的文本:{{ inputText }}</p> </div> </template> <script> import VueSimpleKeyboard from 'vue-simple-keyboard'; export default { components: { VueSimpleKeyboard, }, data() { return { inputText: '', }; }, }; </script> ``` 在上面的代码中,我们引入了 VueSimpleKeyboard 组件,并在模板中使用了它。我们将键盘布局设置为 default,使用 v-model 指令将键盘输入的值绑定到 inputText 变量上,然后将 inputText 变量的值展示在页面上。 除了默认布局,Vue-SimpleKeyboard 还支持多种其他布局,包括 QWERTY、数字、符号等。你可以在组件属性中设置 layout 属性来指定不同的布局。 除了基本的键盘输入功能,Vue-SimpleKeyboard 还支持键盘事件监听和回调函数,可以让你在键盘输入时执行一些操作。例如,你可以在键盘输入完成后触发一个回调函数来处理输入的文本。 总的来说,Vue-SimpleKeyboard 是一个非常实用的虚拟键盘组件,可以用于模拟键盘输入,提供超丝滑的键盘输入体验。如果你需要在 Vue 项目中使用虚拟键盘,Vue-SimpleKeyboard 组件是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值