search
HomeWeb Front-endJS TutorialWhat are jQuery events? Introduction to jquery events

What are jQuery events? Introduction to jquery events

Sep 10, 2018 pm 04:23 PM
csshtmljavascript

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:

<button>按钮</button>
<script>
function click1(){
console .log(&#39;this  is button,&#39;);
}
$(&#39;#btn&#39;).bind(&#39;click&#39;,click1);</script>

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

<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>


<p>菜单</p>
        
  • 北京
  •     
  • 南京
  •     
  • 天津
<script> // mouseover表示鼠标悬停在指定元素之上 mouseout表示鼠标从指定元素上移开 //jQuery支持链式操作,多事件绑定时,事件名称之间使用空格分离。 $(&#39;#title&#39;).bind(&#39;mouseover mouseout&#39;, function(){ if ($(&#39;ul&#39;).is(&#39;:hidden&#39;)) { $(&#39;ul&#39;).css(&#39;display&#39;,&#39;block&#39;); } else { $(&#39;ul&#39;).css(&#39;display&#39;,&#39;none&#39;); } }); /* unbind()方法 1.没有指定任何事件时 - 将指定元素的所有事件全部解绑定 2.指定一个事件名称时 - 将指定元素的指定当个事件解绑定 3.指定多个事件名称时 - 将指定元素的指定多个事件解绑定 */ $(&#39;#title&#39;).unbind(&#39;mouseover mouseout&#39;);<h2>Comparison of event binding methods<p>jQuery provides multiple sets of event binding and unbinding methods <br/>1.bind() and unbind() - methods deleted after jQuery 3.0 version <br/>2.on() and off() methods - New methods after jQuery version 1.7<br/> In fact, the underlying methods of bind() and unbind() are on() and off()<br/>3.live() and die() - Methods deleted after jQuery version 1.7<br/> Function - Implement event delegation <br/>4.delegate() and undelegate() - Methods added after jQuery 1.6 version, jQuery <br/> Method deleted after version 3.0 <br/> Function - Implement event delegation <br/>5. one() - Bind a one-time function to the event <h2>Event switching<h3>hover() method<p>jQuery provides the hover() method to simulate the mouse hover event effect . <br/><pre class='brush:php;toolbar:false;'>$element.hover(over,out);</pre><p>The sample code is as follows: <br/><pre class="brush:php;toolbar:false"> <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; } <body> <p id="title">菜单 <ul> <li>北京 <li>南京 <li>天津 <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>

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
Javascript Data Types : Is there any difference between Browser and NodeJs?Javascript Data Types : Is there any difference between Browser and NodeJs?May 14, 2025 am 12:15 AM

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScript Comments: A Guide to Using // and /* */JavaScript Comments: A Guide to Using // and /* */May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

See all articles

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

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),