jquery



jquery

0 0


jquery


On Github avi-madbhave / jquery

jQuery
avi.madbhave@gmail.com
Agenda
  • Understand me ( DOM )
  • Setup me
  • Use me
  • Find me
  • Animate me
  • Rate me
  • Plug me
  • Handle me
avi.madbhave@gmail.com
Understand me ( DOM )
D is for document :- The DOM can’t work without a document. When we create a web page and load it in a web browser, the DOM stirs into life. It takes the document that we have written and turns into an object.
avi.madbhave@gmail.com
Understand me ( DOM )
O is for object :- Simply objects are self-contained bundles of data.

Variables associated with an object are called properties of the object.

Function that can be executed by an object are called methods of the object.

There are three kinds of objects in jQuery/javascript

  • User-defined
  • Native objects like Array, Math, and Date
  • Host objects like window, document, location, history, XMLHttpRequest, setTimeout
avi.madbhave@gmail.com
Understand me ( DOM )
M is for Model :- The M in DOM stands for Model, but it could just as easily stand for Map. A model, like a map is representation of something. The DOM represents the web page that’s currently loaded in the browser window. The browser provides a map (or model) of the page. We can use jQuery to read this map.

Maps make use of conventions like direction, nodes, and scale. In order to read a map, we need to understand these conventions and it’s the same with the DOM.

The most important convention used by the DOM is the representation of a document as a tree.

avi.madbhave@gmail.com
DOM Structure
avi.madbhave@gmail.com
Setup me
To start jQuery, we need a copy of the jQuery library which we get from the main download page. url : Downloading jQuery

To add jQuery into our web page, we need to used <script> tag

<html>
    <head>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
            // we will add our javascript code here
        </script>
    </head>
    <body>
        <!-- we will add our HTML content here -->
    </body>
</html>
avi.madbhave@gmail.com
Use me
A small Hello jQuery! application.
<html>
    <head>
        <title>Hello jQuery</title>
        <script type=“text/javascript” scr=“jquery.js”></script>
        <script type=“text/javascript”>
            $(document).ready(function() {
                $("a").click(function() {
                    alert("Hello jQuery!");
                });
            });
        </script>
    </head>
    <body>
        <a href=“#”>Click me</a>
    </body>
</html>
avi.madbhave@gmail.com
Find me
jQuery provides two approaches to select elements.

The first uses a combination of CSS and XPath selectors passed as a string to the jQuery constructor (eg. $("div > ul a")):


$(‘.class’)
        $(‘#id’)
        $('tag')
     
    $(‘E[@attr]’)
        $(‘E[@attr=val]’)
        $(‘E[@attr*=val]’)
    
avi.madbhave@gmail.com
Find me
The second uses several methods of the jQuery object:

find()
        next()
        nextAll()
        prev()
        prevAll()
        siblings()
     
    children()
        parent()
        parents()
        not()
        each()
        contents()
    
                    Both approaches can be combined:

$('body').find('a').not('.me');
    
avi.madbhave@gmail.com
Animate me
Simple animations with jQuery can be achieved with show() and hide(). You can create any combination of animations with animate(), eg. a slide with a fade:
$(document).ready(function(){
    $("a.first").click(function(){
        $("div.stuff").toggle();
    });
    $("a.second").click(function() {
        if($('div.stuff:visible').length){
            $('.stuff').hide(1000,"easeInOutExpo");
        } else {
            $('.stuff').show(1000,"easeInOutExpo");
        }
    });
    $("a.third").click(function(){
        $(".stuff").animate({ height: '25px', opacity: '1' }, 'slow');
    });
});
avi.madbhave@gmail.com
Rate me
A small Ajax application, that allows the user to rate something,
$(document).ready(function() {
    //generate markup
    $("#rating").append("Please rate: ");
    for ( var i = 1; i <= 5; i++ ) {
        $("#rating").append("<a href='#'>" + i + "</a>");
    }
    // add markup to container and apply click handlers to anchors
    $("#rating a").click(function(e) {
        // stop normal link click
        e.preventDefault();
        // send request
        $.post("rate.php", {rating: $(this).html()}, function(xml) {
            // format and output result
            $("#rating").html("Thanks for rating, current average: "
                + $("average", xml).text() + ", number of votes: "
                + $("count", xml).text());
        });
    });
});
avi.madbhave@gmail.com
Plug me
Writing your own plugins for jQuery is quite easy. If you stick to the following rules,

Plugin Naming Find a name for your plugin, lets call our example "foobar". Create a file named jquery.[yourpluginname].js, eg. jquery.foobar.js

Adding a Custom Method Create one or more plugin methods by extending the jQuery object, eg.:

    jQuery.fn.foobar = function() {
        // do something
    };
Which will then be accessible by performing:
    $(selector).foobar();
avi.madbhave@gmail.com
Handle me

Bind:- attach the event handler to all of the elements that are matched! That is not good.

Live:- attaches the event handler to the root level document along with the associated selector and event information.

Delegate:- behaves in a similar fashion to the .live() method, but instead of attaching the selector/event information to the document, you can choose where it is anchored.

On:- Lets check what is this

avi.madbhave@gmail.com
Thank you
avi.madbhave@gmail.com
jQuery avi.madbhave@gmail.com