Home  >  Article  >  Web Front-end  >  What are jQuery events? Introduction to jquery events

What are jQuery events? Introduction to jquery events

不言
不言Original
2018-09-10 16:23:141202browse

The content of this article is about jQuery events? The introduction of jquery events has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Page loading

The load event is provided in the DOM for the execution mechanism after the page is loaded. jQuery provides the ready() method to achieve similar functions, but there are the following differences.
1. The load event in DOM does not have any abbreviated form, but the abbreviated form is provided in jQuery's ready() method.
2. The load event will be triggered only after the HTML page is loaded; and after the DOM node tree is loaded, the ready() method will be called.
3. Only one load event can exist in an HTML page, but multiple ready() methods can exist.
The syntax structure of the ready() method:

1.$(document).ready(function(){});
2.$().ready(function(){});//简写
3.$(function(){});//简写

Event binding

Single event binding and single event unbinding

Single event binding

jQuery provides the bind() method to complete the binding event. The syntax is as follows
$element.bind(type,data,callback);
type: indicates the name of the binding event, which is a string type. There is no 'on'.
data: Additional data object (optional) passed to the event object as the element.data property value.
callback: Represents the processing function of the binding event.
The sample code is as follows:

6c04bd5ca3fcae76e30b72ad730ca86d
80833ebf7280790f56f131c64a835e4a按钮65281c5ac262bf6d81768915a4a77ac0
3f1c4e4b6b16bbbd69b2ee476dc4f83a
function click1(){
console .log('this  is button,');
}
$('#btn').bind('click',click1);

Single event unbinding

jQuery provides the unbind() method to unbind events. The specific method is as follows:
$element.unbind(type[,data,callback]);

$('#btn').unbind('click');//只传递事件名称,解绑定该事件的所有处理函数。
$('#btn').undind('click'click1);//传递时间名称和指定的处理函数,解绑定该事件的指定处理函数。

Multiple event binding and unbinding

c9ccee2e6ea535a969eb3f532ad9fe89
        #title {
            width: 100px;
            height: 20px;
            border: 1px solid black;
        }
        ul {
            list-style: none;
            padding: 0;

            display: none;
        }
        li {
            width: 100px;
            height: 20px;
            border: 1px solid black;
        }

    531ac245ce3e4fe3d50054a55f265927
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
0840c441062c67185332150c03ca7be0菜单94b3e26ee717c64999d7867364b1b4a3
ff6d136ddc5fdfeffaf53ff6ee95f185
    25edfb22a4f469ecb59f1190150159c6北京bed06894275b65c1ab86501b08a632eb
    25edfb22a4f469ecb59f1190150159c6南京bed06894275b65c1ab86501b08a632eb
    25edfb22a4f469ecb59f1190150159c6天津bed06894275b65c1ab86501b08a632eb
929d1f5ca49e04fdcb27f9465b944689
3f1c4e4b6b16bbbd69b2ee476dc4f83a
// mouseover表示鼠标悬停在指定元素之上 mouseout表示鼠标从指定元素上移开
 //jQuery支持链式操作,多事件绑定时,事件名称之间使用空格分离。
$('#title').bind('mouseover mouseout', function(){
        if ($('ul').is(':hidden')) {
            $('ul').css('display','block');
        } else {
            $('ul').css('display','none');
        }
    });
 
 /*
        unbind()方法
        1.没有指定任何事件时 - 将指定元素的所有事件全部解绑定
        2.指定一个事件名称时 - 将指定元素的指定当个事件解绑定
        3.指定多个事件名称时 - 将指定元素的指定多个事件解绑定
 */ 
 $('#title').unbind('mouseover mouseout');

Comparison of event binding methods

jQuery provides multiple sets of event binding and unbinding methods
1.bind() and unbind() - methods deleted after jQuery 3.0 version
2.on() and off() methods - New methods after jQuery version 1.7
In fact, the underlying methods of bind() and unbind() are on() and off()
3.live() and die() - Methods deleted after jQuery version 1.7
Function - Implement event delegation
4.delegate() and undelegate() - Methods added after jQuery 1.6 version, jQuery
Method deleted after version 3.0
Function - Implement event delegation
5. one() - Bind a one-time function to the event

Event switching

hover() method

jQuery provides the hover() method to simulate the mouse hover event effect .

$element.hover(over,out);

The sample code is as follows:

   c9ccee2e6ea535a969eb3f532ad9fe89
        #title {
            width: 100px;
            height: 20px;
            border: 1px solid black;
        }
        ul {
            list-style: none;
            padding: 0;

            display: none;
        }
        li {
            width: 100px;
            height: 20px;
            border: 1px solid black;
        }

    531ac245ce3e4fe3d50054a55f265927
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
0840c441062c67185332150c03ca7be0菜单94b3e26ee717c64999d7867364b1b4a3
ff6d136ddc5fdfeffaf53ff6ee95f185
    25edfb22a4f469ecb59f1190150159c6北京bed06894275b65c1ab86501b08a632eb
    25edfb22a4f469ecb59f1190150159c6南京bed06894275b65c1ab86501b08a632eb
    25edfb22a4f469ecb59f1190150159c6天津bed06894275b65c1ab86501b08a632eb
929d1f5ca49e04fdcb27f9465b944689
3f1c4e4b6b16bbbd69b2ee476dc4f83a
 $('#title').hover(function(){
        $('ul').css('display','block');
    },function(){
        $('ul').css('display','none');
    });

2cacc6d41bbb37262a98f745aa00fbf0
36cc49f0c466276486e50c850b7e4956

Event simulation

The trigger() method is provided in jQuery to simulate events bound to division matching elements.
$element.trigger(type[,dat]);

6c04bd5ca3fcae76e30b72ad730ca86d
ddbd97e11a826e220b707861b3166e92按钮65281c5ac262bf6d81768915a4a77ac0
3f1c4e4b6b16bbbd69b2ee476dc4f83a
    // 绑定事件 - 由用户行为进行触发,调用处理函数
    $('#btn').bind('click',function(){
        console.log('this is button.');
    });
    // 模拟用户触发事件
    $('#btn').trigger('click');

2cacc6d41bbb37262a98f745aa00fbf0
36cc49f0c466276486e50c850b7e4956

Related recommendations:

What is event bubbling? How to use jquery to prevent event bubbling_jquery

javascript/jquery keyboard event introduction

The above is the detailed content of What are jQuery events? Introduction to jquery events. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn