Home > Web Front-end > JS Tutorial > body text

Related knowledge points about jQuery events

jacklove
Release: 2018-05-08 11:39:30
Original
1341 people have browsed it

jQuery is specially designed for event processing, which is particularly important in js, so this article will learn about jQuery events.

jQuery event function

jQuery event handling method is the core function in jQuery.

Event handlers refer to methods that are called when certain events occur in HTML. The term "triggered" (or "inspired") by an event is often used.

Usually put the jQuery code in the event handling method in the part:

Example

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){  $("button").click(function(){
    $("p").hide();
  });});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me</button>
</body>
</html>
Copy after login

Try it yourself

above In the example, when the click event of button is triggered, a function will be called:

$("button").click(function() {..some code... } )

This method hides all

elements:

$("p").hide();

in a separate file Functions

If your website contains many pages and you want your jQuery functions to be easy to maintain, put your jQuery functions into separate .js files.

When we demonstrate jQuery in the tutorial, we add the function directly into the section. However, it would be better to put them in a separate file, like this (referencing the file through the src attribute):

Example

<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="my_jquery_functions.js"></script>
</head>
Copy after login

jQuery Name Conflict

jQuery uses the $ symbol as jQuery's Introduction way.

Some functions in other JavaScript libraries (such as Prototype) also use the $ symbol.

jQuery uses a method called noConflict() to solve this problem.

var jq=jQuery.noConflict(), helps you use your own name (such as jq) instead of $ sign.

Conclusion

Since jQuery is specifically designed to handle HTML events, your code will be more appropriate and easier to maintain when you follow these principles:

Put Place all jQuery code in event handlers

Place all event handlers in document ready event handlers

Put jQuery code in separate .js files

If there is a name conflict, rename the jQuery library

This article provides a relevant understanding of jquery events. For more learning materials, please pay attention to the php Chinese website.

Related recommendations:

SQL Server - RDBMS related knowledge

Related knowledge points about SQL NULL values

Explanation of knowledge related to SQL Date function

The above is the detailed content of Related knowledge points about jQuery events. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!