Separates the "behavior" characteristics from the HTML structure.
Separation of behavior from structure -> principle of Unobtrusive JavaScript
jQuery – Description
jQuery Core Library
jQuery UI
The $ function
One of the critical concepts in any jQuery code is the so called ' $ ' function. ' $ ' is actually an 'alias' for the 'jQuery' namespace.
Example 1 : jQuery provides a function for trimming strings. This function can be used as:
str = " foo ";
jQuery . trim (str); // returns "foo"
str = “ foo ";
$ . trim (str);
These are equivalent. Usage of ' $ ' instead of 'jQuery' is an ad-hoc convention, and is considered easier.
Example 2 : To select all the paragraphs that have the class 'foo' and add another class called 'bar' to all of them:
$ (" p . foo ").addClass("bar");
Example 3 : To execute a function 'myfunc' immediately after the page is loaded (called the ready handler in jQuery lingo):
$ ( document ). ready (function() {
myfunc();
});
Use
jQuery usually exists as a single JavaScript file, containing all the common DOM, Event, Effects, and Ajax functions. It can be included within any web page by using the following mark-up:
<script type = "text/javascript" src = "/path/to/jQuery.js" ></script>
The latest stable versions of jQuery can also be loaded using the Google AJAX Libraries API.
$ (" ul .column li . block : first "). addClass (" first ");
//Add a class name of "first" to the first selector
$ (" ul .column li . block : last "). addClass (" last ");
//Add a class name of "last" to the last selector
CSS
. first { background : #ffdbdb ;}
. last {
margin-right : 0;
background : #c4dcff ;
}
Example :-
:odd and :even Selectors
Specify Even and Odd Selectors
$ (" ul li : even "). addClass (" even ");
$ (" ul li : odd "). addClass (" odd ");
CSS
. even {
background : #fff ;
border : 1px solid #ccc ;
}
. odd { background : #ccc ; }
How to get the element ?
The key point you have to learn is how to get the exact element that you want to apply the effects.
$ (" # header ") = get the element with id="header“
$ (" h3 ") = get all <h3> element
$ (" div # content . photo ") = get all element with class="photo" nested in the <div id="content">
$ (" ul li ") = get all <li> element nested in all <ul>
$ (" ul li : first ") = get only the first <li> element of the <ul>
Styles of Interaction
via the $ function, which is a factory method for the jQuery object. These functions, often called commands , are chainable ; they each return the jQuery object
via $. -prefixed functions. These are utility functions which do not work on the jQuery object.
$ (" div . test "). add (" p . quote "). addClass (" blue "). slideDown (" slow ");
Finds the union of all div tags with class attribute test and all p tags with class attribute quote, adds the class attribute blue to each matched element, and then slides them down with an animation. The $ and add functions affect the matched set, while the addClass and slideDown affect the referenced nodes.
0 comments
Post a comment