Beruflich Dokumente
Kultur Dokumente
1.
JQUERY l g? .................................................................................................................. 2
2.
Download jQuery.............................................................................................................. 2
3.
4.
5.
6.
Cu hi n tp ................................................................................................................ 12
7.
JQUERY
Chng ny gii thiu tng quan jQuery.
1. JQUERY l g?
jQuery l mt th vin kiu mi ca Javascript gip n gin ha cch vit JavaScript v tng tc x
l cc s kin trn trang web. Cc th vin jQuery cung cp mt mc ch chung l s tru tng ho cc lp
ph bin cho cc trang web scripting, do n hu ch trong hu ht cc tnh hung scripting.
Cc chc nng jQuery bao gm:
Chn mt tag hoc mt tp hp cc tag trn trang web.
Cung cp cc hm tin ch thng dng.
Nhanh chng to ra cc tag mi.
Quan trng nht l phn tch vic thit k v lp trnh web trn pha client.
2. Download jQuery
jQuery l mt th vin script, do chng ta c th thm vo trang web. Chng ta c th download phin
bn mi nht ca jQuery t http://docs.jquery.com/Downloading_jQuery.
Cc m bn trong s c thc thi khi trang c ti xong. Tuy nhin, m Javascript khng chy cho n
khi tt c cc hnh nh trong trang c ti xong (k c banner qung co). L do cho vic s dng
window.onload l chc chn rng HTML 'document' phi c ti xung ht, sau mi chy m kia.
gii quyt, jQuery c cu lnh n gin, c bit nh:
$(document).ready(function() {
//M ca chng ta
});
M ny kim tra document v i cho n khi n sn sng s dng - theo chng ta mun.
Basic
id #
Tr v mt phn t ca mng: Array <Element>
N tng ng vi mt phn t duy nht c gn bi thuc tnh id.
Nu id cha cc k t nh du chm (.) hoc 2 du [] th chng phi thm 2 du ngch cho (\\)
vo trc cc k t .
Nu l $("#some.id") th phi vit thnh $("#some\\.id")
Nu l $("#some[id]") th phi vit thnh $("#some\\[id]\\")
element
Kiu tr v: Array<Element(s)>
Tng ng vi tt c cc phn t vi tn c gn.
.class
Kiu tr v: Array<Element(s)>
Tng ng vi tt c cc phn t vi tn lp.
*
Kiu tr v: Array<Element(s)>
Tng ng vi tt c cc phn t.
$("div,span").css("background-color", "#EEEEEE");
});
</script>
<style type="text/css" >
div {
float:left;padding: 5px;margin: 5px;
}
.Class1{
}
</style>
</head>
<body >
<div id="div1" align="center" >Visual Studio 2008</div>
<div id="div2" align="center">ASP.NET 3.5</div>
<div id="div3.1.[0]" align="center"> <a
href="http://docs.jquery.com/Downloading_jQuery"> Download jQuery </a></div>
<br />
<span class="Class1">Cho mng bn n vi jQuery</span>
</body>
</html>
5.1.2.
HIERACHY
ancestor descendant
Kiu tr v: Array<Element(s)>
Kiu tr v: Array<Element(s)>
prev + next
Kiu tr v: Array<Element(s)>
</style>
</head>
<body>
<form id="form1" runat ="server" >
<div id="main">Nhp thng tin c nhn </div>
<br />
H v tn:<input type="text" size="50" />
<fieldset>
<legend>Ni cng tc</legend>
<label>Tn cng ty:</label>
<input type="text" size="50" /><br />
<label>a ch:</label>
<input type="text" size="100" /><br />
</fieldset>
<br />
H khu thng tr: <input type="text" size="100" /><br /><br />
<div id="submit">
<button>Cp nht</button>
<button>Hy b</button>
</div>
</form>
</body>
</html>
5.1.3.
:first
Kiu tr v: Array<Element(s)>
:last
Kiu tr v: Array<Element(s)>
:not(selector)
Kiu tr v: Array<Element(s)>
:even
Kiu tr v: Array<Element(s)>
:odd
Kiu tr v: Array<Element(s)>
Tng ng vi cc phn t l, ch s 1, 3, 5,
V d:
$("tr:odd").css("background-color", "#bbbbff");
:eq(index)
Kiu tr v: Array<Element(s)>
:header
Kiu tr v: Array<Element(s)>
V d:
V d: Vi_du_9_3.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Vi_du_9_3</title>
<script src="jquery-1.3.2.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("tr:first").css("font-style", "italic");
$("tr:last").css({ backgroundColor: 'yellow', fontWeight: 'bolder' });
$("tr:even").css("color", "red");
$("tr:odd").css("color", "blue");
$("tr:eq(4)").css("text-decoration", "line-through");
$("tr:gt(6)").css("font-size", "30px");
$("tr:lt(3)").css("font-size", "25px");
$(":header").css({ background: '#CCC', color: 'blue' });
});
</script>
</head>
<body>
<table border="1">
<tr><td>Dng th 1</td></tr>
<tr><td>Dng th 2</td></tr>
<tr><td>Dng th 3</td></tr>
<tr><td>Dng th 4</td></tr>
<tr><td>Dng th 5</td></tr>
<tr><td>Dng th 6</td></tr>
<tr><td>Dng th 7</td></tr>
<tr><td>Dng th 8</td></tr>
<tr><td>Dng th 9</td></tr>
</table>
<h1>Tiu 1</h1>
<p>Ni dung 1</p>
<h2>Tiu 2</h2>
<p>Ni dung 2</p>
<p></p>
<p></p>
</body>
</html>
5.1.4.
:empty
Kiu tr v: Array<Element(s)>
:has(selector)
Kiu tr v: Array<Element(s)>
:parent
Kiu tr v: Array<Element(s)>
V d:
$("td:parent)").css("font-size", "30px");
V d: Vi_du4.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Vi_du_9_4</title>
<script src="jquery-1.3.2.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("div:contains('Th')").css("text-decoration", "underline");
$("td:empty").css("background-color", "#EEEEEE");
$("div:has(p)").css("font-size", "30px");
$("td:parent").css("color", "yellow");
});
</script>
<style type="text/css" >
td {width:200px;background:green; }
</style>
</head>
<body>
<div><h2>Danh sch lp 12A3</h2></div>
<div> Th Thu Hng</div>
<div>L Vn Bnh</div>
<div>Trn Th Hng</div>
<div>Nguyn Hi Nam</div>
<div>Hong Thu Bnh</div>
<br />
<table border="1">
<tr><td>TD #0</td><td></td></tr>
<tr><td>TD #2</td><td></td></tr>
</table>
<div><p>Cho bn n vi jQuery</p></div>
<div>Cho bn n vi jQuery</div>
<div> </div>
<div>
</body>
</html>
5.1.5.
Visibility Filters
:hidden
Kiu tr v: Array<Element(s)>
:visble
Kiu tr v: Array<Element(s)>
V d: Vi_du_9_5.aspx
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Vi_du_9_5</title>
5.2
Attributes
5.2.1
Class
addClass( class )
Kiu tr v: jQuery
$("p").addClass("Maudo");
$("p").removeClass("Maudo");
toggleClass( class )
Kiu tr v: jQuery
V d: Vi_du_9_6.aspx
JQuery
5.2.2
html()
HTML, Text
Kiu tr v: String
$("p").click(function() {alert($(this).html())});
text()
Kiu tr v: String
$("p").click(function() {alert($(this).html())});
5.3
Events
Bind( type, [data], fn )
Bind mt hander vo mt s kin cho mi phn t ph hp, type l s kin, [data] (ty chn)
thm d liu thng qua s kin, fn l hm x l khi s kin xy ra.
JQuery
10
toogle( fn, fn )
Chuyn i gi hm gia 2 ln click.
V d: Vi_du_7.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Vi_du_9_7</title>
<script src="jquery-1.3.2.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("p:first").bind("click", function(e) {
var str = "( " + e.pageX + ", " + e.pageY + " )";
$("span:first").text("S kin click va xy ra ti v tr " + str);
});
$("p:first").bind("dblclick", function() {
$("span:first").text("S kin click p va xy ra ti th " +
this.tagName);
});
var n = 0;
$("div:lt(2)").one("click", function() {
var index = $("div").index(this) + 1;
$(this).css({ borderStyle: "inset",
cursor: "auto"
});
$("p:last").text("Hnh ch nht th " + index + " va c click."
+
"
});
$("button:eq(0)").click(function() {
update($("span:eq(1)"));
});
$("button:eq(1)").click(function() {
$("button:first").trigger('click');
update($("span:eq(2)"));
});
function update(j) {
var n = parseInt(j.text(), 0);
j.text(n + 1);
}
function aClick() {
$("div:last").show().fadeOut("slow");
}
JQuery
11
$("#bind").click(function() {
// could use .bind('click', aClick) instead but for variety...
$("#theone").click(aClick).text("C th click");
});
$("#unbind").click(function() {
$("#theone").unbind('click', aClick)
.text("Khng lm g c....");
});
$("li").hover(
function() {
$(this).css("color", "red");
},
function() {
$(this).css("color", "black");
}
);
});
</script>
<style type ="text/css" >
p { background:yellow; font-weight:bold; cursor:pointer; padding:5px; }
span { color:red; }
.div1 { width:60px; height:60px; margin:5px; float:left;
background:green; border:5px outset;
cursor:pointer; }
</style>
</head>
<body>
<p>Click hoc click p vo y.</p>
<span></span>
<p>Hy click vo cc hnh ch nht mu xanh</p>
<div class="div1" ></div>
<div class="div1"></div>
<br /><br /><br /><br /><br />
<button>Button th 1</button>
<button>Button th 2</button>
<div><span>0</span> ln button th 1 c click.</div>
<div><span>0</span> ln button th 2 c clicks.</div>
<button id="theone">Khng lm g c....</button>
<button id="bind">Bind Click</button>
<button id="unbind">Unbind Click</button>
<div style="display:none;">Click!</div>
<br />
<p>Hy di chuyn chut ln My bay, t, Xe my, Xe p</p>
<ul>
<li>My bay</li>
<li>t</li>
<li>Xe my</li>
<li>Xe p</li>
</ul>
</body>
</html>
6. Cu hi n tp
1. jQuery l g?
Tr l i:
jQuery l mt th vin kiu mi ca Javascript gip n gin ha cch vit JavaScript v tng tc x
l cc s kin trn trang web. Cc th vin jQuery cung cp mt mc ch chung l s tru tng ho cc lp
ph bin cho cc trang web scripting, do n hu ch trong hu ht cc tnh hung scripting.
Cc chc nng jQuery bao gm:
Chn mt tag hoc mt tp hp cc tag trn trang web.
Cung cp cc hm tin ch thng dng.
JQuery
12
JQuery
13