Table of Contents
Page loading
Event binding
Single event binding and single event unbinding
Single event binding
Single event unbinding
Multiple event binding and unbinding
Comparison of event binding methods
Event switching
hover() method
Event simulation
Home Web Front-end JS Tutorial What are jQuery events? Introduction to jquery events

What are jQuery events? Introduction to jquery events

Sep 10, 2018 pm 04:23 PM
css html javascript

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(){});//简写
Copy after login

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:

<body>
<button id=&#39;btn&#39;>按钮</button>
<script>
function click1(){
console .log('this  is button,');
}
$('#btn').bind('click',click1);
Copy after login

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);//传递时间名称和指定的处理函数,解绑定该事件的指定处理函数。
Copy after login

Multiple event binding and unbinding

<style>
        #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;
        }

    </style>
</head>
<body>
<p id="title">菜单</p>
<ul>
    <li>北京</li>
    <li>南京</li>
    <li>天津</li>
</ul>
<script>
// 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');
Copy after login

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);
Copy after login

The sample code is as follows:

   <style>
        #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;
        }

    </style>
</head>
<body>
<p id="title">菜单</p>
<ul>
    <li>北京</li>
    <li>南京</li>
    <li>天津</li>
</ul>
<script>
 $(&#39;#title&#39;).hover(function(){
        $(&#39;ul&#39;).css(&#39;display&#39;,&#39;block&#39;);
    },function(){
        $(&#39;ul&#39;).css(&#39;display&#39;,&#39;none&#39;);
    });

</script>
</body>
Copy after login

Event simulation

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

<body>
<button id="btn">按钮</button>
<script>
    // 绑定事件 - 由用户行为进行触发,调用处理函数
    $(&#39;#btn&#39;).bind(&#39;click&#39;,function(){
        console.log(&#39;this is button.&#39;);
    });
    // 模拟用户触发事件
    $(&#39;#btn&#39;).trigger(&#39;click&#39;);

</script>
</body>
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Apr 18, 2025 am 09:24 AM

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

See all articles