The code for different browser binding events is not the same, so we can block differences between browsers by using jquery to write code.
In jquery, you can use the onTo bind an event, specifying the name of the event and the corresponding handler function:
// 获取超链接的jQuery对象:
var a = $(‘#test-link‘);
a.on(‘click‘, function () {
alert(‘Hello!‘);
});
But generally, we don't use this because we can directly use encapsulated event methods, such as the on click above, which we can write directly:
a.click(function () {
alert(‘Hello!‘);
});
1. Common Event 1.1 mouse events
| Click |
Trigger when mouse clicks |
| DblClick |
Trigger when mouse double-click |
| MouseEnter |
Trigger when Mouse enters |
| MouseLeave |
Trigger when mouse moves out |
| MouseMove |
Triggers when the mouse moves inside the DOM |
| Hover |
Two functions are triggered when the mouse enters and exits, equivalent to MouseEnter plus MouseLeave |
1.2 Keyboard event keyboard events only work on the DOM of the current focus, usually <input> and <textarea>.
| KeyDown |
Triggered when the keyboard is pressed |
| KeyUp |
Trigger when keyboard is released |
| KeyPress |
Triggers after pressing the key once |
1.3 Other events
| Focus |
Triggered when the DOM gets focus |
| Blur |
Triggered when the DOM loses focus |
| Change |
triggered when the content of <input>, <select>, or <textarea> changes |
| Submit |
triggered when <form> commits |
| Ready |
Triggered when the page is loaded and the DOM tree finishes initializing |
Note: Ready is used only for document objects, our JS code is usually waiting for the DOM to be loaded and then executed, or DOM is often not found, so our own initialization code needs to be placed in the ready event, ensuring that the DOM has been initialized:
<html>
<head>
<script>
$(document).on(‘ready‘, function () {
$(‘#testForm).on(‘submit‘, function () {
alert(‘submit!‘);
});
});
</script>
</head>
<body>
<form id="testForm">
...
</form>
</body>
Or:
$(document).ready(function () {
// on(‘submit‘, function)也可以简化:
$(‘#testForm).submit(function () {
alert(‘submit!‘);
});
});
Usually we use the following method, which is more concise:
$(function () {
// init...
});
2, event parameters some events such as MouseMove and keypress, we need to get the position of the mouse and the value of the key. All events will pass inEventObject as a parameter, you can get more information from the object:
$(function () {
$(‘#testMouseMoveDiv‘).mousemove(function (e) {
$(‘#testMouseMoveSpan‘).text(‘pageX = ‘ + e.pageX + ‘, pageY = ‘ + e.pageY);
});
});
As shown, the x, Y coordinate values of the mouse movement are constantly changing: 3. Unbind an event that has been bound to unbind it byoff (' xxx ', function)Implementation, it is worth noting that the off-mode unbind is unable to cancel the direct event method, and the following unbind is not valid:
// 绑定事件:
a.click(function () {
alert(‘hello!‘);
});
// 解除绑定:
a.off(‘click‘, function () {
alert(‘hello!‘);
});
This is a two different function object, so off cannot cancel the first anonymous function that has been banding.
You can use off (' click ') to remove all functions of the bound click event, or Off () to remove all types of event handlers that have been bound.
4. Event Trigger conditions Remember,The triggering of an event is always caused by a user action, that is, if you useJS code to change the value, does not trigger the corresponding event!
If you must use the JS trigger event, you can directly invoke the non-parametric event method to triggerFor example:
var input = $(‘#test-input‘);
input.val(‘change it!‘);
input.change(); // 触发change事件
// input.change()相当于input.trigger(‘change‘),它是trigger()方法的简写
In addition, some JS code even if set trigger, also because the browser security settings can not be implemented, can only be triggered by the user.
06jquery-05-Events