KEMBAR78
Working With JQuery Part1 | PPTX
Seçil AYDIN
secil.aydin@argela.com.tr
ï‚Ą   Developed by John Resig, 2006
ï‚Ą   Open Source Javascript Library
ï‚Ą   Two versions: compressed and
    uncompressed
ï‚Ą   MIT and GPL license
ï‚Ą   WRITE LESS, DO MORE
ï‚Ą   Light and small size
ï‚Ą   Plugins (http://plugins.jquery.com)
ï‚Ą   Easy to learn
ï‚Ą   CSS3 support
ï‚Ą   Large documentation
    (http://docs.jQuery.com)
ï‚Ą   Availability of examples
ï‚Ą   Google
ï‚Ą   Amazon.com
ï‚Ą   IBM, Dell
ï‚Ą   Microsoft
ï‚Ą   NBC
ï‚Ą   EA Games
ï‚Ą   Twitter, Facebook, 

ï‚Ą   For other companies:
    http://docs.jquery.com/Sites_Using_jQuery
ï‚Ą   Connect to official web site:
    http://jquery.com/ and download compressed
    version
        <!DOCTYPE html>
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>My First Example</title>
        <script src="src/scripts/jquery-1.6.4.min.js"></script>
        <script>
        $(document).ready(function){
        //jquery codes will be here
        }
        </script>
        </head>
        <body>

        </body>
        </html>
ï‚Ą   Use jQuery or $ sign
     jQuery('div').html('Selam'); OR        $('div').html('Selam');

    for(var i=1; i<$(‘div’).length;i++) OR var num = $(‘div’).length;
                                                    for(var i=1; i<num; i++)


                      $(‘div’).toggleClass(‘blue_dot’);




                   Selector    Method       Paramter
1                                            2
$(document).ready(function(){                   $().ready(function(){
//code                                          //code
});                                             });

                                                  4
                                        function($){
                                        //code
                     3
                                        })(jQuery);
            $function(){
            //code
            });


     Different styles of document ready (run when page is loaded)
1
             function add(){                        3
                               $.transactions = {
             }                 add: function(){
             add();            },

                               take_square: function(num) {
                               alert(num * num);
               2
                               }
$.add = function(){
                               };
                               $.transactions.add();
};
                               $.transactions.take_square(4);
jQuery.add(); or $.add();
Let’s do it!
$(“p”).click(function(){
//code
});

Or

function action(){
//code
}

$(“p”).click(action);
ï‚Ą    Basic Selectors
1)   Tag Selectors
2)   Class Selectors
3)   #id Selectors


1)Tag Selectors:
<a>, <p>, <table>, <h1>, <img>, <span>, <div>,
   <form>, <select>, etc.
Ex: $(‘p’).css(‘text-decoration’, ‘underline’);
(second.html)
2) Class Selectors:
Select according to class
$('.m1').css('text-decoration','underline');
$('li.m3').css('text-decoration', 'underline');

3) #id Selectors:
$('#mobile').css('text-decoration','underline');
$('#list .m3').css('text-decoration','underline');
ï‚Ą  Hierarchy Selectors
1) Descendant Selectors(E F):
grandfather-descendant relationship
$('body ul').css('color', 'blue');
2) Next Adjacent Selectors(E+F):
select next matching next
$('div+p').css('color','yellow');
$('ul li+p').css('color','yellow');
$('#mobile+div').css('color','yellow');
3) Next Siblings Selectors(E~F/ Prev Siblings):
$('div~p').css('color','pink');
$('p~p').css('color','pink');
4) Child Selectors(E>F/ parent>child):
$('body>p').css('color', 'yellow');
$('ul >li').css('color','yellow');
5) E,F,G Selectors:
$('#mobile, #list').css('color', 'pink');
$('.m2, .p1').css('color','blue');
ï‚Ą Attribute Selectors
$('div[id="layer"]').css('color','red');
$('a[href^="http"][href$="com"]').css('color','yell
  ow');
    Selection Method           Description(selects)
    $(a[name]);                links with name property
    $('a[name="contact"]');    links with contact name
    $('a[name^="cont"]');      links with beginning with cont name
    $('a[name$="act"]');       links with ends with act name
    $('a[name!="contact"]');   links with not containing contact
    $('a[name*="ont"]');       links with containing ont
Form Object             Method
   text         $('input[type="text"]');
 password     $('input[type="password"]');
    file         $('input[type="file"]');
  hidden       $('input[type="hidden"]');
 checkbox     $('input[type="checkbox"]');
   radio        $('input[type="radio"]');
  select       $('input[type="select"]');
  image        $('input[type="image"]');
  submit       $('input[type="submit"]');
   reset        $('input[type="reset"]');
  button       $('input[type="button"]');
 textarea     $('input[type="textarea"]');
ï‚Ą   Simple Filters:
Use Simple Selectors or hierarchical selectors
Uses index number an object of DOM
Indexes starting from 0
Filters starting with : Filter        Description
                         :first            Selects object with 0 index number
                         :last             with last index number
                         :not              with not cont. property
                         :even             with even index number
                         :odd              with odd index number
                         :eq(n), :nth(n)   with specified index number
                         :gt(n)            with greater index than specified
                         :lt(n)            with lesser index than specified
                         :header           with header like <h1>, <h2>
Filter          Description

:nth-child(n)   select object with n index number between selected objects


:first-child    select first object between selected objects


:last-child     select last object between selected objects


:only-child     select only the one object between selected objects
Filter        Description

:contains()   selects objects with the specified contents

:empty        selects object with empty objects (opposite of parent)

:has()        selects objects with objects

:parent       selects objects with objects (opposite of empty)
Filter     Description

:hidden    selects hidden objects

:visible   selects visible objects
Filter      Description

:enabled    selects active objects

:disabled   selects passive objects

:selected   takes the value of open able object

:checked    takes the value of check box
Filter                     Description

:eq(index), .eq(-index)    selects object with the entered index

.has()                     selects objects inside the selected object

.filter(selector)          entered object/objects are selected. Also makes entered function
.filter(function(index))
.is()                      checks whether selected object has entered property

.each()                    builds an loop inside the selected object

.first()                   returns the first object of the selector object

.last()                    returns the last object of the selector object

.map()                     converts the objects inside the selector

.not()                     returns the objects not suits the selection

.slice()                   selects objects between two index points
ï‚Ą   .EQ()
<ul>
   <li>Star TV</li>
   <li>ShowTV</li>
   <li>ATV</li>
   <li>KanalD</li>
   <li>TRT1</li>
</ul>

$(‘li’).eq(1); //selects Star TV
$(‘li’).eq(-1); //selects TRT1

Ps: difference between :eq() is taking negative number.
ï‚Ą   .HAS()
<div>
   <span>Panasonic</span>
</div>
<div>
   <label>Sony</label>
</div>

$(‘div’).has(“span”); //selects Panasonic

Ps: difference between :has() is taking parameter with (“”)
$(“ul”).has(“li”) function has better performance than
$(“ul:has(li)”)
See examples included.

Working With JQuery Part1

  • 1.
  • 2.
    ï‚Ą Developed by John Resig, 2006 ï‚Ą Open Source Javascript Library ï‚Ą Two versions: compressed and uncompressed ï‚Ą MIT and GPL license ï‚Ą WRITE LESS, DO MORE
  • 3.
    ï‚Ą Light and small size ï‚Ą Plugins (http://plugins.jquery.com) ï‚Ą Easy to learn ï‚Ą CSS3 support ï‚Ą Large documentation (http://docs.jQuery.com) ï‚Ą Availability of examples
  • 4.
    ï‚Ą Google ï‚Ą Amazon.com ï‚Ą IBM, Dell ï‚Ą Microsoft ï‚Ą NBC ï‚Ą EA Games ï‚Ą Twitter, Facebook, 
 ï‚Ą For other companies: http://docs.jquery.com/Sites_Using_jQuery
  • 5.
    ï‚Ą Connect to official web site: http://jquery.com/ and download compressed version <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>My First Example</title> <script src="src/scripts/jquery-1.6.4.min.js"></script> <script> $(document).ready(function){ //jquery codes will be here } </script> </head> <body> </body> </html>
  • 6.
    ï‚Ą Use jQuery or $ sign jQuery('div').html('Selam'); OR $('div').html('Selam'); for(var i=1; i<$(‘div’).length;i++) OR var num = $(‘div’).length; for(var i=1; i<num; i++) $(‘div’).toggleClass(‘blue_dot’); Selector Method Paramter
  • 7.
    1 2 $(document).ready(function(){ $().ready(function(){ //code //code }); }); 4 function($){ //code 3 })(jQuery); $function(){ //code }); Different styles of document ready (run when page is loaded)
  • 8.
    1 function add(){ 3 $.transactions = { } add: function(){ add(); }, take_square: function(num) { alert(num * num); 2 } $.add = function(){ }; $.transactions.add(); }; $.transactions.take_square(4); jQuery.add(); or $.add();
  • 9.
  • 10.
  • 11.
    ï‚Ą Basic Selectors 1) Tag Selectors 2) Class Selectors 3) #id Selectors 1)Tag Selectors: <a>, <p>, <table>, <h1>, <img>, <span>, <div>, <form>, <select>, etc. Ex: $(‘p’).css(‘text-decoration’, ‘underline’); (second.html)
  • 12.
    2) Class Selectors: Selectaccording to class $('.m1').css('text-decoration','underline'); $('li.m3').css('text-decoration', 'underline'); 3) #id Selectors: $('#mobile').css('text-decoration','underline'); $('#list .m3').css('text-decoration','underline');
  • 13.
    ï‚Ą HierarchySelectors 1) Descendant Selectors(E F): grandfather-descendant relationship $('body ul').css('color', 'blue'); 2) Next Adjacent Selectors(E+F): select next matching next $('div+p').css('color','yellow'); $('ul li+p').css('color','yellow'); $('#mobile+div').css('color','yellow');
  • 14.
    3) Next SiblingsSelectors(E~F/ Prev Siblings): $('div~p').css('color','pink'); $('p~p').css('color','pink'); 4) Child Selectors(E>F/ parent>child): $('body>p').css('color', 'yellow'); $('ul >li').css('color','yellow'); 5) E,F,G Selectors: $('#mobile, #list').css('color', 'pink'); $('.m2, .p1').css('color','blue');
  • 15.
    ï‚Ą Attribute Selectors $('div[id="layer"]').css('color','red'); $('a[href^="http"][href$="com"]').css('color','yell ow'); Selection Method Description(selects) $(a[name]); links with name property $('a[name="contact"]'); links with contact name $('a[name^="cont"]'); links with beginning with cont name $('a[name$="act"]'); links with ends with act name $('a[name!="contact"]'); links with not containing contact $('a[name*="ont"]'); links with containing ont
  • 16.
    Form Object Method text $('input[type="text"]'); password $('input[type="password"]'); file $('input[type="file"]'); hidden $('input[type="hidden"]'); checkbox $('input[type="checkbox"]'); radio $('input[type="radio"]'); select $('input[type="select"]'); image $('input[type="image"]'); submit $('input[type="submit"]'); reset $('input[type="reset"]'); button $('input[type="button"]'); textarea $('input[type="textarea"]');
  • 17.
    ï‚Ą Simple Filters: Use Simple Selectors or hierarchical selectors Uses index number an object of DOM Indexes starting from 0 Filters starting with : Filter Description :first Selects object with 0 index number :last with last index number :not with not cont. property :even with even index number :odd with odd index number :eq(n), :nth(n) with specified index number :gt(n) with greater index than specified :lt(n) with lesser index than specified :header with header like <h1>, <h2>
  • 18.
    Filter Description :nth-child(n) select object with n index number between selected objects :first-child select first object between selected objects :last-child select last object between selected objects :only-child select only the one object between selected objects
  • 19.
    Filter Description :contains() selects objects with the specified contents :empty selects object with empty objects (opposite of parent) :has() selects objects with objects :parent selects objects with objects (opposite of empty)
  • 20.
    Filter Description :hidden selects hidden objects :visible selects visible objects
  • 21.
    Filter Description :enabled selects active objects :disabled selects passive objects :selected takes the value of open able object :checked takes the value of check box
  • 22.
    Filter Description :eq(index), .eq(-index) selects object with the entered index .has() selects objects inside the selected object .filter(selector) entered object/objects are selected. Also makes entered function .filter(function(index)) .is() checks whether selected object has entered property .each() builds an loop inside the selected object .first() returns the first object of the selector object .last() returns the last object of the selector object .map() converts the objects inside the selector .not() returns the objects not suits the selection .slice() selects objects between two index points
  • 23.
    ï‚Ą .EQ() <ul> <li>Star TV</li> <li>ShowTV</li> <li>ATV</li> <li>KanalD</li> <li>TRT1</li> </ul> $(‘li’).eq(1); //selects Star TV $(‘li’).eq(-1); //selects TRT1 Ps: difference between :eq() is taking negative number.
  • 24.
    ï‚Ą .HAS() <div> <span>Panasonic</span> </div> <div> <label>Sony</label> </div> $(‘div’).has(“span”); //selects Panasonic Ps: difference between :has() is taking parameter with (“”) $(“ul”).has(“li”) function has better performance than $(“ul:has(li)”)
  • 25.

Editor's Notes

  • #4 Example: $(‘div:even’).addClass(‘yesil_alan’);Add yesil_alan style to all divs with the even index.
  • #7 Which one is better?
  • #12 DOM Document Object Model
  • #14 $(&apos;div+p&apos;).css(&apos;color&apos;,&apos;yellow&apos;); selects second one of the two objects at the same level$(&apos;ul li+p&apos;).css(&apos;color&apos;,&apos;yellow); nothing selected because li and ul inside the div and p are on the same level$(&apos;#mobile+div&apos;).css(&apos;color&apos;,&apos;yellow&apos;); they are on the same level color will be yellow
  • #15 $(&apos;div~p&apos;).css(&apos;color&apos;,&apos;pink&apos;); selects divs on the same level$(&apos;p~p&apos;).css(&apos;color&apos;,&apos;pink&apos;); selects next p on the same level$(&apos;body&gt;p&apos;).css(&apos;color&apos;, &apos;yellow&apos;); selects all child objects inside the first object$(&apos;ul &gt;li&apos;).css(&apos;color&apos;,&apos;yellow&apos;); selects all li objects inside ul object$(&apos;#mobile, #list&apos;).css(&apos;color&apos;, &apos;pink&apos;); selects all objects in order$(&apos;.m2, .p1&apos;).css(&apos;color&apos;,&apos;blue&apos;); selects objects with the class m2 and p1
  • #16 $(&apos;div[id=&quot;layer&quot;]&apos;).css(&apos;color&apos;,&apos;red&apos;); //selects the divs with the id layer$(&apos;a[href^=&quot;http&quot;][href$=&quot;com&quot;]&apos;).css(&apos;color&apos;,&apos;yellow&apos;); //selectshref links beginning with http and ending with com and finally add css as yellow