Home  >  Article  >  Web Front-end  >  Introduction to the use of jquery events and functions_jquery

Introduction to the use of jquery events and functions_jquery

WBOY
WBOYOriginal
2016-05-16 17:21:121093browse
1. Bind events

Method 1:

Use this method to add events to elements when the page loads
Copy code The code is as follows:

$("#myElement").click(function(){
alert($(this). text());
})

Method 2:

Same as 1, you can also add events to elements after the page is loaded, such as creating new DOM elements
Copy code The code is as follows:

$("#myElement").bing('click' ,function(){
alert($(this).text());
})

2. Function declaration and named function expression

Function declaration:
Copy code The code is as follows:

function myFun1() {
$("div").hide();
}

Call: myFun1();

Function expression:
Copy code The code is as follows:

var myFun2 = function(){
$("div").hide ();
}

Call: #("#myElement").click(myFun2);

Difference: The difference in calling time, Function declarations can be used anywhere, named function expressions can only be used after declaration
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