Dasar Jquery Bagian 1 (Hide and show)
$("#id") tag ini akan mencari tag yang menggunakan atribut id=id sama juga dengan getElementById().
$(".id") tag ini akan mencari tag yang menggunakan atribut class=id sama juga dengan getElementByClassName().
$("div") tag ini akan mencari tag yang menggunakan atribut div ,<div></div> atau sama dengan getElementByTagName("div").
Animasi :
1. Hide and Show :
HTML(jquery.html)
<!DOCTYPE html>
<html>
<head>
<style>
.anim { background:#000; padding:3px; float:left; color:#FFF; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script> // Ini adalah plugin jquery yang membuat pergerakan animasi menjadi lebih halus
</head>
<body>
<button id="hidr">Hide</button>
<button id="showr">Show</button>
<div>
<span class="anim">Suatu saat nanti saya akan menjadi programmer hebat</span>
</div>
<script>
$("#hidr").click(function () {
$(".anim").hide("slow")});
$("#showr").click(function () {
$(".anim").show(1000);
});
</script>
</body>
</html>
Example :
Suatu saat nanti saya akan menjadi programmer hebat
Dasar Jquery bagian 2 (Fade in fade out animation)
HTML (Fade.html)
<!DOCTYPE html>
<html>
<head>
<style>
.box,
#btn1,#btn2 { float:left; margin:5px 10px 5px 0; }
.box { width:80px; background:#090; text-align:center; padding:20px 0
20px 0; color:#FFF; font-family:Arial, Helvetica, sans-serif; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<button id="btn1">fade out</button>
<button id="btn2">show</button>
<br>
<br>
<div id="box1" class="box">box1</div>
<script>
$("#btn1").click(function() {
function complete() {
$("<div/>").text(this.id);
}
$("#box1").fadeOut(1600, "linear", complete);
});
$("#btn2").click(function() {
$("#box1").fadeIn(1600);
});
</script>
</body>
</html>
Example :
Dasar Jquery bagian 3 (Slide in and Slide out)
HTML (slide.html)
<!DOCTYPE html>
<html>
<head>
<style>
.box,
#btn1,#btn2 { float:left; margin:5px 10px 5px 0; }
.box { width:80px; background:#090; text-align:center; padding:20px 0
20px 0; color:#FFF; font-family:Arial, Helvetica, sans-serif; }
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script></head>
<body>
<button id="btn1">slide out</button>
<button id="btn2">slide in</button>
<br>
<br>
<div id="box1" class="box">box1</div>
<script>
$(document).ready(function() {
$("#btn2").click(function () {
$("#box1").show("slide", { direction: "down" }, 1000);
});
$("#btn1").click(function () {
$("#box1").hide("slide", { direction: "down" }, 1000);
});
});
</script>
</body>
</html>
Jquery Bagian 4 Moving Element. appendTo()
Script html biasa (html.html)
<html>
<head>
<style>
p { background:#999; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<p>Kata terakhir saya: </p>
</body>
</html>
Hasil :
Kata terakhir saya:
Setelah html ditambah jquery append
<html>
<head>
<style>
p { background:#ff0000; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<p>Kata terakhir : </p>
<script>
$("p").append(document.createTextNode("Hai"));
</script>
</body>
</html>
Hasil:
Kata terakhir saya: Hai
Kesimpulan : append() jquery digunakan untuk menambah property pada sebuah tag, dalam script di atas tag <p> ditambah dengan text Hai.
Jquery Bagian 5 Moving Element. appendTo()
HTML (jquery.html)
<!DOCTYPE html>
<html>
<head>
<style>#append { background:#333;
color:#FFF;
width:270px; }</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<span class="append">sedang tidak melakukan apa apa...</span>
<div id="append">SAYA </div>
<script>$(".append").appendTo("#append"); </script>
</body>
</html>
Hasil :
SAYA sedang tidak melakukan apa apa...
Kesimpulan :
Jquery appendTo() digunakan untuk menggabungkan semua properti (style, text) kedua tag .
Pada contoh diatas tag <span class="append"> dengan style kosong dan text
"sedang tidak melakukan apa apa..."
digabungkan dengan tag <div id="append"> dengan style
{background:#333; color:#FFF; width:270px;}
dan text "SAYA" menjadi :
SAYA sedang tidak melakukan apa apa...
Jquery bagian 6 Moving Element .before() dan .after()
Script HTML biasa (jquery.html)
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>Saya halim...</p><b>Halo</b>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>Saya halim...</p><b>Halo</b>
</body>
</html>
Hasil :
Saya halim...
Halo
Setelah diberi jquery before (jquery.html)
Saya halim...
Halo
Setelah diberi jquery before (jquery.html)
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<p>Saya halim...</p><b>Halo</b>
<script>$("p").before( $("b") );</script>
</body>
</html>
Hasil :
Halo
Saya halim...
Kesimpulan : Tag jquery before() membuat tag <b>Halo</b> diletakkan sebelum tag <p>Saya halim...</p>.
Setelah diberi jquery before (jquery.html)
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<b>Halo</b><p>Saya halim...</p>
<script>$("p").after( $("b") );</script>
</body>
</html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<p>Saya halim...</p><b>Halo</b>
<script>$("p").before( $("b") );</script>
</body>
</html>
Hasil :
Halo
Saya halim...
Kesimpulan : Tag jquery before() membuat tag <b>Halo</b> diletakkan sebelum tag <p>Saya halim...</p>.
Setelah diberi jquery before (jquery.html)
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<b>Halo</b><p>Saya halim...</p>
<script>$("p").after( $("b") );</script>
</body>
</html>
Hasil :
Saya halim...
Halo
Kesimpulan : Tag jquery before() membuat tag <b>Halo</b> diletakkan sesudah tag <p>Saya halim...</p>.
Saya halim...
Halo
Kesimpulan : Tag jquery before() membuat tag <b>Halo</b> diletakkan sesudah tag <p>Saya halim...</p>.
Jquery bagian 7 Attributes .attr()
HTML ( jquery.html )
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<img title="cake.jpg"/>
<script>
$("img").attr("src", function() {
return this.title;
});
</script>
</body>
</html>
Kesimpulan : jquery attr() digunakan untuk manambahkan atribut pada sebuah tag, contoh di atas tag <img title="cake.jpg"/> di beri script berikut :
<script>
$("img").attr("src", function() {
return this.title;
});
</script>
Maka tag Image akan menjadi seperti ini <img src="cake.jpg" title="cake.jpg">.
Download script
Kesimpulan : Jquery css() digunakan untuk menambah atau mengganti style css suatu tag , contoh di atas tag <p> dengan style p{ color :green }. Ditambah fungsi hover dengan style :
'background-color' : 'yellow', 'font-weight' : 'bolder'
dan lain lain.
HTML (jquery.html)
<!DOCTYPE html>
<html>
<head>
<style>
p { margin: 8px; font-size:16px; }
.selected { color:red; }
.highlight { background:yellow; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<p>Hello</p>
<script>
$("p").addClass("selected highlight");
</script>
</body>
</html>
Kesimpulan : Jquery addClass() digunakan untuk menggabungkan style css dalam suatu tag, contoh diatas tag <p> dengan style p { margin: 8px; font-size:16px; }
digabung dengan .selected { color:red; } .highlight { background:yellow; }.
Jquery bagian 8 Attributes .css()
HTML (jquery.html)
<!DOCTYPE html>
<html>
<head>
<style>
p { color:green; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<p>Move the mouse over a paragraph.</p>
<p>Like this one or the one above.</p>
<script>
$("p").hover(function () {
$(this).css({'background-color' : 'yellow', 'font-weight' : 'bolder'});
}, function () {
var cssObj = {
'background-color' : '#ddd',
'font-weight' : '',
'color' : 'rgb(0,40,244)'
}
$(this).css(cssObj);
});
</script>
</body>
</html>
<html>
<head>
<style>
p { color:green; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<p>Move the mouse over a paragraph.</p>
<p>Like this one or the one above.</p>
<script>
$("p").hover(function () {
$(this).css({'background-color' : 'yellow', 'font-weight' : 'bolder'});
}, function () {
var cssObj = {
'background-color' : '#ddd',
'font-weight' : '',
'color' : 'rgb(0,40,244)'
}
$(this).css(cssObj);
});
</script>
</body>
</html>
Hasil :
Move the mouse over a paragraph.
Like this one or the one above.
Kesimpulan : Jquery css() digunakan untuk menambah atau mengganti style css suatu tag , contoh di atas tag <p> dengan style p{ color :green }. Ditambah fungsi hover dengan style :
'background-color' : 'yellow', 'font-weight' : 'bolder'
dan lain lain.
Jquery bagian 9 Attributes .addClass()
<!DOCTYPE html>
<html>
<head>
<style>
p { margin: 8px; font-size:16px; }
.selected { color:red; }
.highlight { background:yellow; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<p>Hello</p>
<script>
$("p").addClass("selected highlight");
</script>
</body>
</html>
Hasil :
Hello
Kesimpulan : Jquery addClass() digunakan untuk menggabungkan style css dalam suatu tag, contoh diatas tag <p> dengan style p { margin: 8px; font-size:16px; }
digabung dengan .selected { color:red; } .highlight { background:yellow; }.
Tidak ada komentar:
Posting Komentar