JQuery is specially designed for the event handler.
JQuery event function
JQuery event handling is core functions in jQuery.
The event handler is called when some event occurs in the HTML.The terminology "trigger" will often be used.
Will usually be the jQuery code into <head> part of the event handler method:
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>
In the above example,When the button click event is triggered will be call a function:
$("button").click(function() {..some code... } )
The method hides all the <p> elements:
$("p").hide();
Function in a separate file
If your web site contains a lot of pages,and you hope your jQuery functions easy to maintain,So please put your jQuery functions into the separate file.
When we demonstrate the jQuery in the tutorial,will be the function added to the <head> section,however,the best put it into a separate file, like this(via the src attribute to reference the file ):
Example:
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="my_jquery_functions.js"></script>
</head>
JQuery name conflicts
The jQuery use the $ symbol as a jQuery short way.
Some other functions same use $ symbol in the javascript library.
The jQuery use name is noConflict() function to resolve this issue.
var jq=jQuery.noConflict(),help you use yourself name (such as jq) to replace $ symbol.
JQuery events
The following jQuery event example:
Event functions | binding the functions to |
---|---|
$(document).ready(function) | bind a function to the document ready event |
$(selector).click(function) | Trigger or bind a function to the click event of selected element. |
$(selector).dblclick(function) | Trigger or bind a function to the double click event of selected element. |
$(selector).focus(function) | Trigger or bind a function to the get focus event of selected element. |
$(selector).mouseover(function) | Trigger or bind a function to the mouse over event of selected element. |