1. 程式人生 > >Vue之鍵盤事件

Vue之鍵盤事件

http col 說明 enter color view ble logs sca

1.使用keydown觸發事件

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title></title>  
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">  
    <meta name="apple-mobile-web-app-capable"
content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <style> </style> <script src="vue.js"></script> <script> window.onload=function(){ new Vue({ el:#box, data:{ }, methods:{ show:
function(){ alert(1); } } }); }; </script> </head> <body> <div id="box"> <input type="text" @keydown="show"> </div> </body> </html>

描述:

按下鍵盤觸發show方法,彈出框1.

結果:

技術分享

2.按下鍵盤彈出事件的鍵碼

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title></title>  
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">  
    <meta name="apple-mobile-web-app-capable" content="yes">  
    <meta name="apple-mobile-web-app-status-bar-style" content="black">  
    <style>  
  
    </style>  
    <script src="vue.js"></script>  
    <script>  
        window.onload=function(){  
            new Vue({  
                el:#box,  
                data:{  
  
                },  
                methods:{  
                    show:function(ev){  
                        alert(ev.keyCode);  
                    }  
                }  
            });  
        };  
    </script>  
</head>  
<body>  
    <div id="box">  
        <input type="text" @keydown="show($event)">  
    </div>  
</body>  
</html>  

3.keyup事件

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title></title>  
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">  
    <meta name="apple-mobile-web-app-capable" content="yes">  
    <meta name="apple-mobile-web-app-status-bar-style" content="black">  
    <style>  
  
    </style>  
    <script src="vue.js"></script>  
    <script>  
        window.onload=function(){  
            new Vue({  
                el:#box,  
                data:{  
  
                },  
                methods:{  
                    show:function(ev){  
                        alert(ev.keyCode);  
                    }  
                }  
            });  
        };  
    </script>  
</head>  
<body>  
    <div id="box">  
        <input type="text" @keyup="show($event)">  
    </div>  
</body>  
</html>  

4.Vue中在鍵盤事件後面添加鍵碼值就可以說明是按下了哪個鍵

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title></title>  
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">  
    <meta name="apple-mobile-web-app-capable" content="yes">  
    <meta name="apple-mobile-web-app-status-bar-style" content="black">  
    <style>  
  
    </style>  
    <script src="vue.js"></script>  
    <script>  
        window.onload=function(){  
            new Vue({  
                el:#box,  
                data:{  
  
                },  
                methods:{  
                    show:function(){  
                        alert(您按回車了);  
                    }  
                }  
            });  
        };  
    </script>  
</head>  
<body>  
    <div id="box">  
        <input type="text" @keyup.13="show()">  
    </div>  
</body>  
</html>  

4.使用鍵名enter

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title></title>  
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">  
    <meta name="apple-mobile-web-app-capable" content="yes">  
    <meta name="apple-mobile-web-app-status-bar-style" content="black">  
    <style>  
  
    </style>  
    <script src="vue.js"></script>  
    <script>  
        window.onload=function(){  
            new Vue({  
                el:#box,  
                data:{  
  
                },  
                methods:{  
                    show:function(){  
                        alert(您按回車了);  
                    }  
                }  
            });  
        };  
    </script>  
</head>  
<body>  
    <div id="box">  
        <input type="text" @keyup.enter="show()">  
    </div>  
</body>  
</html>  

5.使用鍵名left

<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <title></title>  
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">  
    <meta name="apple-mobile-web-app-capable" content="yes">  
    <meta name="apple-mobile-web-app-status-bar-style" content="black">  
    <style>  
  
    </style>  
    <script src="vue.js"></script>  
    <script>  
        window.onload=function(){  
            new Vue({  
                el:#box,  
                data:{  
  
                },  
                methods:{  
                    show:function(){  
                        alert(1);  
                    }  
                }  
            });  
        };  
    </script>  
</head>  
<body>  
    <div id="box">  
        <input type="text" @keyup.left="show()">  
    </div>  
</body>  
</html>  

Vue之鍵盤事件