Home Web Front-end JS Tutorial Different jQuery APIs to handle different browser events_jquery

Different jQuery APIs to handle different browser events_jquery

May 16, 2016 pm 05:47 PM
api jquery

Handling events is a bit difficult part of today's web browsers, and different browsers handle events in different ways. Therefore, to overcome these cross-browser issues, one can leverage jQuery's event handling API.

jQuery is a small JavaScript library that provides a vast API to handle different browser events and effects and much more. Read more about handling browser user interface effects using JavaScript. In this tutorial, we will explore the different APIs of jQuery to handle different browser events.
Page Load Event
Ready (FN),
This is the basis of all types of events that jQuery supports. You may want to set the focus of the form when the page is loaded or do some UI effects.

Copy code The code is as follows:

$(document).ready(function () {
$("p").text("The DOM is now loaded and can be manipulated.");
});

Event handling
Binding (Type, Data, FN),
You may want to bind an element to any element that handles one or more events (click/double click, etc.). Use this feature in conjunction with custome's event handler for any element.
Copy code The code is as follows:

$("p").bind("click" , function(e) {
var str = "( " e.pageX ", " e.pageY " )";
$("span").text("Click happened! " str);
});
$("p").bind("dblclick", function() {
$("span").text("Double-click happened in " this.tagName);
});
$("p").bind("mouseenter mouseleave", function(e) {
$(this).toggleClass("over");
});

trigger (event, data)
triggers the event on every matched element,
this will also cause the browser to have the same name (if one exists) The default action performed. For example, a function via trigger() will also cause the browser to submit a "submit" of the form. False default behavior can be prevented by returning one of the functions bound to the event.
The triggered events are not limited to browser-based events. You can also customize event trigger binding registration.
Copy code The code is as follows:

$("button:first").click(function () {
update($("span:first"));
});
$("button:last").click(function () {
$("button: first").trigger('click');
update($("span:last"));
});
function update(j) {
var n = parseInt(j .text(), 10);
j.text(n 1);
}

Interactive auxiliary activities
In today’s Web 2.0 applications Program, user intraction processing is very important. jQuery provides some APIs that can be used to handle these interactions. Hover (Multiple) This function provides hover functionality, i.e. when the mouse cursor moves over a matching element, the first specified function is fired. When the mouse moves over the element, the second specified function fires. Also, check where you see if the mouse is still over the specified element (e.g. an image inside a div), if so it will continue to "hover" and not move out (a common behavior when using the mouseout event handler) error.
Copy code The code is as follows:

$("li").hover(
function () {
$(this).append($(" ***"));
},
function () {
$( this).find("span:last").remove();
}
);

Auxiliary for other activities
The following are the functions, Can be used to handle different types of event lists.
blur()
: Triggers the blur event for each matching element.
Copy code The code is as follows:

$("input").blur(function () {
$(this).next("span").css('display','inline').fadeOut(1000);
});
Blur (FN)
: Bind a handler function to the blur event of each matching element.
[code]
$("input").blur(function () {
$(this).next("span").css('display','inline').fadeOut( 1000);
});

(FN)
: Bind a function to the change event of each matching element.
Copy code The code is as follows:

$("select").change(function () {
var str = "";
$("select option:selected").each(function () {
str = $(this).text() " ";
});
$("div").text(str);
})
.change();

Click (FN)
: Function bound to the click event of each matching element.
Copy code The code is as follows:

$("p").click(function () {
$(this).slideUp();
});
$("p").hover(function () {
$(this).addClass("hilite");
}, function () {
$(this).removeClass("hilite");
});

Double-click (FN)
: Trigger the DblClick event for each matching element.
Copy code The code is as follows:

var divdbl = $("div:first");
divdbl.dblclick(function () {
divdbl.toggleClass('dbl');
});

Key (FN)
: Bind a function to the keypress event of each matching element.
Copy code The code is as follows:

$("input").keypress(function (e ) {
if (e.which == 32 || (65 <= e.which & e.which <= 65 25)
|| (97 <= e.which && e.which <= 97 25)) {
var c = String.fromCharCode(e.which);
$("p").append($(""))
.children(":last")
.append(document.createTextNode(c));
} else if (e.which == 8) {
// backspace in IE only be on keydown
$("p").children(":last").remove();
}
$("div").text(e.which);
});

mousedown (FN)
: Function bound to the mousedown event of each matched element.
Copy code The code is as follows:

$("p").mouseup(function() {
$(this).append('Mouse up.');
}).mousedown(function(){
$(this).append('Mouse down.');
});

Scroll (FN)
: Bind a handler function to the scroll event of each matching element.
Copy code The code is as follows:

$("p").clone().appendTo (document.body);
$("p").clone().appendTo(document.body);
$("p").clone().appendTo(document.body);
$(window).scroll(function () {
$("span").css("display", "inline").fadeOut("slow");
});
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
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)

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

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

Oracle API Usage Guide: Exploring Data Interface Technology Oracle API Usage Guide: Exploring Data Interface Technology Mar 07, 2024 am 11:12 AM

Oracle is a world-renowned database management system provider, and its API (Application Programming Interface) is a powerful tool that helps developers easily interact and integrate with Oracle databases. In this article, we will delve into the Oracle API usage guide, show readers how to utilize data interface technology during the development process, and provide specific code examples. 1.Oracle

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

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: &lt

Oracle API integration strategy analysis: achieving seamless communication between systems Oracle API integration strategy analysis: achieving seamless communication between systems Mar 07, 2024 pm 10:09 PM

OracleAPI integration strategy analysis: To achieve seamless communication between systems, specific code examples are required. In today's digital era, internal enterprise systems need to communicate with each other and share data, and OracleAPI is one of the important tools to help achieve seamless communication between systems. This article will start with the basic concepts and principles of OracleAPI, explore API integration strategies, and finally give specific code examples to help readers better understand and apply OracleAPI. 1. Basic Oracle API

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

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:

How to deal with Laravel API error problems How to deal with Laravel API error problems Mar 06, 2024 pm 05:18 PM

Title: How to deal with Laravel API error problems, specific code examples are needed. When developing Laravel, API errors are often encountered. These errors may come from various reasons such as program code logic errors, database query problems, or external API request failures. How to handle these error reports is a key issue. This article will use specific code examples to demonstrate how to effectively handle Laravel API error reports. 1. Error handling in Laravel

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

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

Understand the role and application scenarios of eq in jQuery Understand the role and application scenarios of eq in jQuery Feb 28, 2024 pm 01:15 PM

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

See all articles