What events are there in vuejs
The events in vuejs are: focus, blur, click (click), dblclick, contextmen, mousemove, mouseover, mouseout, mouseup, keydown, keyup, select, wheel, etc.
The operating environment of this tutorial: windows7 system, vue2.9.6 version, DELL G3 computer.
Event Handler
Event Handling
Event handling in Vuejs is very powerful and very important. We must learn it well.
Event Handler
The reason why Vuejs puts it in a high position is based on this consideration:
Put the code related to the event Written independently, it is very easy to locate various logics and easy to maintain. After
event handler
is separated, theDOM
element of the page will look very simple. Easy to understand.When a page is closed, the corresponding
ViewModel
will also be recycled. Then the variousevent handler
defined on the page will also be garbage collected. Will not cause memory overflow.
SupportedEvent
We have seen before I’ve been to v-on:click
, so what events can be supported by v-on
?
As long as it is a standard HTML defined Event
, it is supported by Vuejs.
-
focus
(The element gains focus) -
blur
(The element loses focus) -
click
(Click the left mouse button) -
dblclick
(Double-click the left mouse button) -
contextmenu
(Single-machine right mouse button) -
mouseover
(The pointer moves to the element with event monitoring or its child elements) -
mouseout
(The pointer moves out of the element, or to its child elements Up) -
keydown
(Keyboard action: Press any key) -
keyup
(Keyboard action: Release any key)
All HTML standard events: https://developer.mozilla.org/zh-CN/docs/Web/Events
Example:
A total of 162 standard events are defined, and dozens of non-standard events, as well as Mozilla-specific events. As shown in the figure below:
We don’t need to remember them all. Usually in daily development, less than 20 are the most common events.
Usev-on
Binding events
We can think that almost all events are driven by v-on
this directive
. Therefore, this section will provide a more detailed explanation of v-on
.
1. Use variables in v-on
As shown in the following code, you can reference variables in v-on
:
<html> <head> <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script> </head> <body> <div id='app'> 您点击了: {% raw %}{{{% endraw %} count }} 次 <br/> <button v-on:click='count += 1' style='margin-top: 50px'> + 1</button> </div> <script> var app = new Vue({ el: '#app', data: { count: 0 } }) </script> </body> </html>
After opening the above code with a browser and clicking the button, you can see that the variable count
will be 1. As shown in the figure below:
2. Use the method name
in
v-on. The above example can also be implemented as follows:
<html> <head> <script ></script> </head> <body> <div id='app'> 您点击了:{% raw %}{{{% endraw %} count }} 次 <br/> <button v-on:click='increase_count' style='margin-top: 50px'> + 1 </button> </div> <script> var app = new Vue({ el: '#app', data: { count: 0 }, methods: { increase_count: function(){ this.count += 1 } } }) </script> </body> </html>
You can see that in v-on:click='increase_count'
, increase_count
is a method name.
3. Use the method name parameter in v-on
We can also use v-on:click=' directly some_function("your_parameter")'
is written like this, as shown in the following example:
<html> <head> <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script> </head> <body> <div id='app'> {% raw %}{{{% endraw %} message }} <br/> <button v-on:click='say_hi("明日的Vuejs大神")' style='margin-top: 50px'> 跟我打个招呼~ </button> </div> <script> var app = new Vue({ el: '#app', data: { message: "这是个 在click中调用 方法 + 参数的例子" }, methods: { say_hi: function(name){ this.message = "你好啊," + name + "!" } } }) </script> </body> </html>
After opening it with a browser, click the button, and you will see the following picture:
4. Redesign the logic of the button
In actual development, we often encounter situations like this: Click a button, or trigger a certain After the event, you want the default state of the button.
The most typical example: When submitting a form (<form/>
), we want to verify the form first. If verification fails, the form is not submitted.
At this time, if we want the form not to be submitted, we have to make this submit
button without any next action. In all development languages, there will be a corresponding method called: "preventDefault
"
(Stop the default action)
Let's look at this example:
<html> <head> <script ></script> </head> <body> <div id='app'> 请输入您想打开的网址, <br/> 判断规则是: <br/> 1. 务必以 "http://"开头 <br/> 2. 不能是空字符串 <br/> <input v-model="url" placeholder="请输入 http:// 开头的字符串, 否则不会跳转" /> <br/> <br/> <a v-bind:href="this.url" v-on:click='validate($event)'> 点我确定 </a> </div> <script> var app = new Vue({ el: '#app', data: { url: '' }, methods: { validate: function(event){ if(this.url.length == 0 || this.url.indexOf('http://') != 0){ alert("您输入的网址不符合规则。 无法跳转") if(event){ alert("event is: " + event) event.preventDefault() } } } } }) </script> </body> </html>
上面的代码中,可以看到,我们定义了一个变量: url
. 并且通过代码:
<a v-bind:href="this.url" v-on:click='validate($event)'> 点我确定 </a>
做了两件事情:
把
url
绑定到了该元素上。该元素 在触发
click
事件时,会调用validate
方法。 该方法传递了一个特殊的参数:$event
. 该参数是当前 事件的一个实例。(MouseEvent
)
在 validate
方法中,我们是这样定义的: 先验证是否符合规则。 如果符合,放行,会继续触发 <a/>
元素的默认动作(让浏览器发生跳转) 。 否则的
话,会弹出一个 “alert
” 提示框。
用浏览器打开这段代码,可以看到下图所示:
我们先输入一个合法的地址: http://baidu.com , 可以看到,点击后,页面发生了跳转。 跳转到了百度。
我们再输入一个 “不合法”的地址: https://baidu.com 注意: 该地址不是以 “http://” 开头,所以我们的vuejs
代码不会让它放行。
如下图所示:
进一步观察,页面也不会跳转(很好的解释了 这个时候 <a/>
标签点了也不起作用)
5. Event Modifiers
事件修饰语
我们很多时候,希望把代码写的优雅一些。 使用传统的方式,可能会把代码写的很臃肿。 如果某个元素在不同的event
下有不同的表现,那么代码看起来就会有
很多个 if ...else ...
这样的分支。
所以, Vuejs 提供了 “Event Modifiers
”。
例如,我们可以把上面的例子略加修改:
<html> <head> <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script> </head> <body> <div id='app'> 请输入您想打开的网址, <br/> 判断规则是: <br/> 1. 务必以 "http://"开头 <br/> 2. 不能是空字符串 <br/> <input v-model="url" placeholder="请输入 http:// 开头的字符串, 否则不会跳转" /> <br/> <br/> <a v-bind:href="this.url" v-on:click='validate($event)' v-on:click.prevent='show_message'> 点我确定 </a> </div> <script> var app = new Vue({ el: '#app', data: { url: '' }, methods: { validate: function(event){ if(this.url.length == 0 || this.url.indexOf('http://') != 0){ if(event){ event.preventDefault() } } }, show_message: function(){ alert("您输入的网址不符合规则。 无法跳转") } } }) </script> </body> </html>
可以看出,上面的代码的核心是:
<a v-bind:href="this.url" v-on:click='validate($event)' v-on:click.prevent='show_message'> 点我确定 </a> methods: { validate: function(event){ if(this.url.length == 0 || this.url.indexOf('http://') != 0){ if(event){ event.preventDefault() } } }, show_message: function(){ alert("您输入的网址不符合规则。 无法跳转") } }
先是在 <a/>
中定义了两个 click 事件,一个是 click
, 一个是 click.prevent
. 后者表示,如果该元素的click 事件被 阻止了的话, 应该触发什么动作。
然后,在 methods
代码段中,专门定义了 show_message
, 用来给 click.prevent
所使用。
上面的代码运行起来,跟前一个例子是一模一样的。 只是抽象分类的程度更高了一些。 在复杂的项目中有用处。
这样的 “event modifier”,有这些:
- stop propagation 被停止后( 也就是调用了 event.stopPropagation()方法后 ),被触发
- prevent 调用了 event.preventDefault() 后被触发。
- capture 子元素中的事件可以在该元素中 被触发。
- self 事件的 event.target 就是本元素时,被触发。
- once 该事件最多被触发一次。
- passive 为移动设备使用。 (在addEventListeners 定义时,增加passive选项。)
以上的 “event modifier” 也可以连接起来使用。 例如: v-on:click.prevent.self
6. Key Modifiers
按键修饰语
Vuejs 也很贴心的提供了 Key Modifiers, 也就是一种支持键盘事件的快捷方法。 我们看下面的例子:
<html> <head> <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script> </head> <body> <div id='app'> 输入完毕后,按下回车键,就会<br/> 触发 "show_message" 事件~ <br/><br/> <input v-on:keyup.enter="show_message" v-model="message" /> </div> <script> var app = new Vue({ el: '#app', data: { message: '' }, methods: { show_message: function(){ alert("您输入了:" + this.message) } } }) </script> </body> </html>
可以看到,在上面的代码中, v-on:keyup.enter="show_message"
为 <a></a>
元素定义了事件,该事件对应了 “回车键”。
(严格的说,是回车键被按下后,松开弹起来的那一刻)
我们用浏览器打开上面的代码对应的文件,输入一段文字,按回车,就可以看到事件已经被触发了。
Vuejs 总共支持下面这些 Key modifiers:
- enter 回车键
- tab tab 键
- delete 同时对应了 backspace 和 del 键
- esc ESC 键
- space 空格
- up 向上键
- down 向下键
- left 向左键
- right 向右键
随着 Vuejs 版本的不断迭代和更新,越来越多的 Key modifiers 被添加了进来, 例如 page down
, ctrl
。对于这些键的用法,
大家可以查阅官方文档。
相关推荐:《vue.js教程》
The above is the detailed content of What events are there in vuejs. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





On iPhones running iOS 16 or later, you can display upcoming calendar events directly on the lock screen. Read on to find out how it's done. Thanks to watch face complications, many Apple Watch users are used to being able to glance at their wrist to see the next upcoming calendar event. With the advent of iOS16 and lock screen widgets, you can view the same calendar event information directly on your iPhone without even unlocking the device. The Calendar Lock Screen widget comes in two flavors, allowing you to track the time of the next upcoming event, or use a larger widget that displays event names and their times. To start adding widgets, unlock your iPhone using Face ID or Touch ID, press and hold

The integration of Vue.js and Lua language, best practices and experience sharing for building a front-end engine for game development Introduction: With the continuous development of game development, the choice of game front-end engine has become an important decision. Among these choices, the Vue.js framework and Lua language have become the focus of many developers. As a popular front-end framework, Vue.js has a rich ecosystem and convenient development methods, while the Lua language is widely used in game development because of its lightweight and efficient performance. This article will explore how to

How to use Vue to implement QQ-like chat bubble effects In today’s social era, the chat function has become one of the core functions of mobile applications and web applications. One of the most common elements in the chat interface is the chat bubble, which can clearly distinguish the sender's and receiver's messages, effectively improving the readability of the message. This article will introduce how to use Vue to implement QQ-like chat bubble effects and provide specific code examples. First, we need to create a Vue component to represent the chat bubble. The component consists of two main parts

How to use PHP and Vue.js to implement data filtering and sorting functions on charts. In web development, charts are a very common way of displaying data. Using PHP and Vue.js, you can easily implement data filtering and sorting functions on charts, allowing users to customize the viewing of data on charts, improving data visualization and user experience. First, we need to prepare a set of data for the chart to use. Suppose we have a data table that contains three columns: name, age, and grades. The data is as follows: Name, Age, Grades Zhang San 1890 Li

When a value is added to the input box, the oninput event occurs. You can try running the following code to understand how to implement oninput events in JavaScript - Example<!DOCTYPEhtml><html> <body> <p>Writebelow:</p> <inputtype="text"

Integration of Vue.js and Dart language, practice and development skills for building cool mobile application UI interfaces Introduction: In mobile application development, the design and implementation of the user interface (UI) is a very important part. In order to achieve a cool mobile application interface, we can integrate Vue.js with the Dart language, and use the powerful data binding and componentization features of Vue.js and the rich mobile application development library of the Dart language to build Stunning mobile application UI interface. This article will show you how to

jQuery is a popular JavaScript library that can be used to simplify DOM manipulation, event handling, animation effects, etc. In web development, we often encounter situations where we need to change event binding on select elements. This article will introduce how to use jQuery to bind select element change events, and provide specific code examples. First, we need to create a dropdown menu with options using labels:

How to implement calendar functions and event reminders in PHP projects? Calendar functionality and event reminders are one of the common requirements when developing web applications. Whether it is personal schedule management, team collaboration, or online event scheduling, the calendar function can provide convenient time management and transaction arrangement. Implementing calendar functions and event reminders in PHP projects can be completed through the following steps. Database design First, you need to design a database table to store information about calendar events. A simple design could contain the following fields: id: unique to the event
