jQuery Presentation.pptx

43
Pemrograman Web B 5113100113 Muhamad Luthfie La Roeha 5113100145 Daniel Bintar 5113100171 Nugroho Wicaksono

Transcript of jQuery Presentation.pptx

Page 1: jQuery Presentation.pptx

Pemrograman Web B

5113100113 Muhamad Luthfie La Roeha

5113100145 Daniel Bintar

5113100171 Nugroho Wicaksono

Page 2: jQuery Presentation.pptx

What Is jQuery?

“jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. With a combination of versatility and extensibility, jQuery has changed the way that millions of people write JavaScript.”

- jQuery.com

Page 3: jQuery Presentation.pptx

What You Should Already Know

Page 4: jQuery Presentation.pptx

What It Can Do

• HTML event methods• Effects and animations• HTML/DOM manipulation• CSS manipulation• AJAX• Utilities

*Tip: In addition, jQuery has plugins for almost any task out there.

Page 5: jQuery Presentation.pptx

How To Use It

• Download the jQuery library from jQuery.com• Include jQuery from a CDN, like Google

• Code:<head><script src="jquery.min.js"></script></head>

OR<head><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script></head>

Page 6: jQuery Presentation.pptx

Syntax

• Sintaks jQuery dibuat khusus untuk selecting elemen HTML dan melakukan action pada elemen HTML.• Syntax:• $(selector).action()

• $ untuk mendefinisikan/mengakses jQuery• (selector) untuk mencari/memilih elemen HTML• Suatu action() jQuery yang akan dilakukan pada elemen

yang di pilih.

Page 7: jQuery Presentation.pptx

Why Always Document Ready

• $(document).ready(function(){

   // jQuery methods go here...

});

• Terdapat kemungkinan beberapa action gagal untuk dijalankan• Untuk mencegah suatu kode jQuery berjalan sebelum

halaman berhasil di load/ready.

Page 8: jQuery Presentation.pptx

Selectors

• Salah satu bagian paling penting dalam mempelajari jQuery.• jQuery Selectors = CSS selectors

• Note :• $(this) berarti memilih elemen HTML itu sendiri.

Page 9: jQuery Presentation.pptx

Events and Effects

Page 10: jQuery Presentation.pptx

jQuery Events

Mouse :

• click

• Dblclick

• mouseenter

• mouseleave

Keyboard :

• keypress

• Keydown

• keyup

Form Events :

• Submit

• Change

• Focus

• blur

Documents :

• load

• rezise

• scroll

• unload

• ready

Page 11: jQuery Presentation.pptx

jQuery Events - Mouse

• click ()

syntax : $(selector).click()

Example :

$('div').click(function()

{

alert("You clicked me.");

});

Page 12: jQuery Presentation.pptx

jQuery Events - Mouse

• mouseenter ()

syntax : $(selector).mouseenter()

Example :

$('div').mouseenter(function()

{

alert("You entered me");

});

Page 13: jQuery Presentation.pptx

jQuery Events - Mouse

• mouseleave ()

syntax : $(selector).mouseleave()

Example :

$('div').mouseleave(function()

{

alert("Bye! You now leave me.");

});

Page 14: jQuery Presentation.pptx

jQuery Events - Keyboard

• keypress ()

syntax : $(selector).keypress()

Example :

i=0;

$("input").keypress(function(){

$("span").text(i += 1);

});

Page 15: jQuery Presentation.pptx

jQuery Events - Form

• submit ()

syntax : $(selector).submit()

Example :

$("form").submit(function(){

alert("Submitted");

});

Page 16: jQuery Presentation.pptx

jQuery Effect

• Hide/show

• Fade

• Slide

• Animate

• Stop

• Callback

• Chaining

Page 17: jQuery Presentation.pptx

jQuery Effect - Hide and Show

• Hide () /show ()

syntax : $(selecter).hide(); $(selecter).show();

Example :

$("div").click(function()

{

$(this).hide("fast");

});

$('#action').click(function()

{

$("#red,#yellow,#blue,#green").show(2000);

});

Page 18: jQuery Presentation.pptx

jQuery Effect - Sliding

• slideDown()

• slideUp()

• slideToggle()

syntax : $(selecter).slideDown();

Example :

$("#flip").click(function(){

$("#panel").slideToggle("slow");

});

Page 19: jQuery Presentation.pptx

jQuery Effect - Animation

• animate ()

syntax : $(selecter).animate();

Example :

$("div").click(function()

{

$(this).animate(

{

height: '130px',

width: '130px',

opacity : 0.5

});

});

Page 20: jQuery Presentation.pptx

jQuery Effect – Stop Animations

• stop ()

syntax : $(selector).stop();

Example :

$("#flip").click(function(){

$("#panel").slideDown(5000);

});

$("#action").click(function(){

$("#panel").stop();

});

Page 21: jQuery Presentation.pptx

jQuery Effect : Callback Functions

• callback

syntax : $(selector).action(speed,callback);

Example :

$("#action").click(function(){

$("#red").hide("slow", function(){

alert("Red Box is now hidden");

});

});

Page 22: jQuery Presentation.pptx

jQuery Effect - Chaining

• chaining

Example :

$("#action").click(function(){

$("#panel").slideDown("slow").slideUp("slow");

});

Page 23: jQuery Presentation.pptx

jQuery HTML

• Get

• Set

• Add

• Remove

• Css classes

• Css

• dimensions

Page 24: jQuery Presentation.pptx

jQuery HTML – Get Content and Attributes• Text ()

• Html ()

• Val ()

Syntax : $(selector).action();

Example :

$("#action").click(function(){

alert("Text: " + $("#panel").text());

alert("HTML: " + $("#panel").html());

});

Page 25: jQuery Presentation.pptx

jQuery HTML - Set Content and Attributes• Text ()

• Html ()

• Val ()

Syntax : $(selector).action();

Example :

$("#action").click(function(){

$("#flip").text("Created By : ");

});

Page 26: jQuery Presentation.pptx

jQuery HTML - Add Elements

• Append ()

• Prepend ()

• After ()

• Before ()

Syntax : $(selector).action();

Example :

$("#action").click(function(){

$("#flip").append(" Kelompok 4 ,");

});

Page 27: jQuery Presentation.pptx

jQuery HTML - Remove Elements

• Remove

• Empty

Syntax : $(selector).remove(); $(selector).empty();

Example :

$("#action").click(function(){

$("div").remove();

});

Page 28: jQuery Presentation.pptx

jQuery HTML - Get and Set CSS Classes• addClass()

• removeClass()

• toggleClass()

• css ()

Example :

$("div").click(function(){

$(this).addClass("blue");

});

Page 29: jQuery Presentation.pptx

jQuery HTML - Get and Set CSS

• css()

syntax : $(selector).css();

Example :

$("div").click(function(){

alert("Background color = " + $(this).css("background-color"));

});

Page 30: jQuery Presentation.pptx

jQuery HTML - Dimensions

• Width()

• Height()

• innerWidth()

• innerHeight()

• outerWidth()

• outerHeight()

Example :

$("div").click(function(){var txt = "";

txt += "Width: " + $(this).width();

txt += ", InnerWidth: " + $(this).innerWidth();

txt += ", Height: " + $(this).height();

txt += ", InnerHeight: " + $(this).innerHeight();

alert(txt);

});

Page 31: jQuery Presentation.pptx

Traversing dan AJAX

Page 32: jQuery Presentation.pptx

yang berarti "bergerak melalui“ digunakan untuk memilih elemen HTML berdasarkan hubungan mereka dengan elemen yang lainnya.

jQuery Traversing

Page 33: jQuery Presentation.pptx

Intro

Elemen <div> adalah parent dari <ul> dan ancestor dari semua yang berada di dalamnya.

…. Kedua elemen <li> adalah siblings Elemen <b> adalah child dari <li> kanan dan descendant dari <ul> dan <div>

Page 34: jQuery Presentation.pptx

• Traversing Up• parent()• parents() / parents("ul“)• parentsUntil(“div”)

Macam-macam Traversing

Page 35: jQuery Presentation.pptx

• Traversing Down• children() / children(“p.1”)• find(“span”) / find(“*”)

Macam-macam Traversing

Page 36: jQuery Presentation.pptx

• Traversing Sideaways• siblings()• next()• nextAll()• nextUntil(selector)• prev()• prevAll()• prevUntil(selector)

Macam-macam Traversing

Page 37: jQuery Presentation.pptx

• Traversing Filtering• first()• last()• eq() *eq(0) == first()• filter(".intro")• not(".intro")

Penggunaan filter dan not terlihat saat chaining.

Macam-macam Traversing

Page 38: jQuery Presentation.pptx

AJAX = Asynchronous JavaScript and XML.

AJAX menload data di background dan menampilkannya di halaman web tanpa mereload keseluruhan halaman tersebut.

Contoh : Gmail, Google Maps, Youtube dan Facebook tabs.

jQuery AJAX

Page 39: jQuery Presentation.pptx

• load()• Syntax:

$(selector).load(URL,data,callback);

• Macam-macam callback :• responseTxt - contains the resulting content if the call succeeds• statusTxt - contains the status of the call• xhr - contains the XMLHttpRequest object

• Contoh :$("#div1").load("demo_test.txt #p1");$("#div1").load("demo_test.txt", function(responseTxt, statusTxt, xhr));

Macam-macam AJAX

Page 40: jQuery Presentation.pptx

• get()• Syntax :$(selector).get(URL,callback);

• Contoh : $.get("demo_test.php", function(data, status){        alert("Data: " + data + "\nStatus: " + status);});

Macam-macam AJAX

Page 41: jQuery Presentation.pptx

• post()• Syntax:$(selector).post(URL,data,callback);

• Contoh : $.post("demo_test_post.php",    {

name: "Donald Duck", city: "Duckburg"

},    function(data, status){

alert("Data: " + data + "\nStatus: " + status); }

);

Macam-macam AJAX

Page 42: jQuery Presentation.pptx
Page 43: jQuery Presentation.pptx

Live Coding !