Detailed description of jquery events and binding events
Jquery events and binding events are both very important knowledge. This article mainly introduces the relevant knowledge of jquery events and binding events, which has a good reference value. Let's take a look with the editor below, I hope it can help everyone.
1. First, let’s take a look at the commonly used ways to add events:
<input type="button" id="btn" value="click me!" onclick="shao();" /> <script type="text/javascript"> function shao() { alert("msg is showing!"); } </script>
The most commonly used way for us to add events is to add onclick element attributes to elements
The disadvantage of this method is:
can only be used for one event processing function. In the event processing function method, the way to obtain the event object is different.
Events in jQuery
ready event:
When the page is loaded, execute the function:
<script> $(document).ready(function(e){ alert(document.getElementById("aa").innerHTML); //若是要写function方法,不可以在里面写 }) //要在外面写 </script>
This method can be called wherever it is written;
Mouse event :
<script> $("#aa").click(function(){ alert("点击事件"); }) $("#aa").dblclick(function(){ alert("双击事件"); }) $("#aa").mouseover(function(){ alert("鼠标移上") }); $("#aa").mouseout(function(){ alert("鼠标离开"); }) $("#aa").mousemove(function(){ alert("鼠标移动"); }) $("#aa").mouseup(function(){ alert("鼠标抬起"); }) $("#aa").mousedown(function(){ alert("鼠标按下"); }) 键盘按键按下:给id加没有作用,需要给整个页面加所以用$(document) $(document).KeyEvent(function(){ alert("鼠标离开"); }) </script>
Form element events:
<script> $("#shao").focus(function(){ alert("获得焦点"); }) $("#shao").blur(function(){ alert("失去焦点"); }) $("#shao").change(function(){ alert("值发生变化,change事件"); }) $("#shao").keydown(function(){ alert("键盘按下"); }) </script>
2. Binding events (hanging events):
Events that can dynamically change buttons;
What Is it dynamic binding?
Dynamic binding refers to dynamically added DOM nodes or html elements, which do not exist when they are initially run. If you want to add events to these dynamically added nodes, you must use jquery's on method to bind the events.
bind() adds one or more event handlers to the matching element.
Usage:
$(selector).bind(event,data,function)
Note: The bind() function can only handle events for existing elements. Set up the
code: first write two buttons:
<body> <p id="aa" style="width: 100px; height: 100px; background-color: blueviolet;">hello</p> <!--<input type="text" id="shao" />--> <input type="button" id="btn1" value="挂事件" /> <input type="button" id="btn2" value="移除事件" /> </body>
First operate the click and hang event:
<script> //挂事件, $("#btn1").click(function(){ //点击挂事件,给p绑定一个事件: $("#aa").bind("click",function(){ //bind绑定事件 alert("点击"); }); //括号里两个参数,第一个是事件类型(事件名称),第二个参数是要执行的代码 }) </script>
In this case, click and hang the event:
Remove event button:
<script> //移除事件; $("#aa").click(function(){ //点击移除事件;把p里面的事件移除掉 $("#aa").unbind("click"); //unbind移除绑定,填一个参数,要移除哪个事件 }) </script>
Click to remove, click the event to cancel aa
3. Event data
General events include events Source and time data:
Event data: At this time, the data will be passed
js simplified, you don’t need to write the event source, because you can get
4. JSON syntax:
JSON structure:
Json is simply an object and an array in JavaScript, so these two structures are objects and arrays, which can be represented by these two structures various complex structures.
(1) Object: The object is represented in js as the content enclosed by "{}", and the data structure is the key-value pair structure of {key: value, key: value,...}, in In object-oriented languages, key is the attribute of the object, and value is the corresponding attribute value, so it is easy to understand. The value method is object.key to obtain the attribute value. The type of this attribute value can be a number, a string, an array, or an object. Several kinds.
(2) Array: The array in js is the content enclosed by square brackets "[]". The data structure is ["java", "javascript", "vb",...], and the value is The method is the same as in all languages, using index acquisition. The type of field value can be numbers, strings, arrays, or objects.
Complex data structures can be combined through the two structures of objects and arrays.
json is a lightweight data exchange format
Quanpin:
JavaScript Object Notation
Definition syntax:
var j = { "one":"111111", "two":"22222" };
Value method:
Index:
//数组的取值方式: alert(j["one"]);//直接取索引的方法
Dot syntax:
//点语法: alert(j.one);
JSON can also be a two-dimensional array:
var j = { "one":"111111", "two":"22222", "three":{"aa":"33333"}, }; //数组的取值方式: //alert(j["one"]);//直接取索引的方法 //点语法: alert(j.one); alert(j.three.aa);
Traverse JSON data:
//遍历 for(var v in j) { //定义一个变量v,把j拿到v里面,关键字不是”as“了,是”in“, alert(v); // 这样便利的是索引 alert(j[v]); // 这样是根据索引来取值 }
json does not have a length attribute, so the for loop is not suitable for json
But for-in is also suitable for arrays
Related recommendations;
Detailed examples of the properties of jQuery event objects
The difference between mouseover and mouseenter in jQuery events
When using on to bind events in jQuery What needs attention
The above is the detailed content of Detailed description of jquery events and binding events. 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

AI Hentai Generator
Generate AI Hentai for free.

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

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s

Methods for building event-based applications in PHP include using the EventSourceAPI to create an event source and using the EventSource object to listen for events on the client side. Send events using Server Sent Events (SSE) and listen for events on the client side using an XMLHttpRequest object. A practical example is to use EventSource to update inventory counts in real time in an e-commerce website. This is achieved on the server side by randomly changing the inventory and sending updates, and the client listens for inventory updates through EventSource and displays them in real time.

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute
