Professional Documents
Culture Documents
Young.J
http://jquery.com
jQuery prototype js css XPath
js js js jQuery
Starterkit http://jquery.bassistance.de/jquery-starterkit.zip
jQuery Downloads http://jquery.com/src/
$("p")$("#p"),$("p") p
(<p></p>),$("#p") id "p"(<span id="p"></span>).
jQuery :
1:
2:DOM
3:css
4:javascript
5:
6:event
7:ajax
8:
1. $(expr)
css Xpath html jQuery
expr html
jQuery
<p>one</p>
<div>
<p>two</p>
</div>
<p>three</p>
<a href="#" id="test" onClick="jq()" >jQuery</a>
jQuery
function jq(){
alert($("div > p").html());
}
id test two div p
function jq(){
$("<div><p>Hello</p></div>").appendTo("body");
}
id test body <div><p>Hello</p></div>
2. $(elem)
jQuery dom xml windows
elem jQuery DOM
jQuery
<p>one</p>
<div>
<p>two</p>
</div>
<p>three</p>
<a href="#" id="test" onClick="jq()">jQuery</a>
jQuery
function jq(){
alert($(document).find("div > p").html());
}
id test two div p
function jq(){
$(document.body).background("black");
}
id test
3. $(elems)
jQuery DOM
elem jQuery DOM
jQuery
<form id="form1">
<input type="text" name="textfield">
<input type="submit" name="Submit" value="">
</form>
<a href="#" id="test" onClick="jq()">jQuery</a>
jQuery
function jq(){
$(form1.elements ).hide();
}
id test form1
4. $(fn)
$(document).ready()$(fn)
fn (Function):
$( function(){
$(document.body).background("black");
})
onLoad
5. $(obj)
jQuery
jQuery
<p>one</p>
<div>
<p>two</p>
</div>
<p>three</p>
<a href="#" id="test" onClick="jq()">jQuery</a>
jQuery
function jq(){
var f = $("div");
alert($(f).find("p").html())
}
id test two div p
6. each(fn)
fn (Function):
jQuery
<img src="1.jpg"/>
<img src="1.jpg"/>
<a href="#" id="test" onClick="jq()">jQuery</a>
jQuery
function jq(){
$("img").each(function(){
this.src = "2.jpg"; });
}
id test img src 2.jpg
7. eq(pos)
dom
pos (Number): 0
jQuery
8. get() get(num)
get(num)
get (Number): 0
jQuery
<p>This is just a test.</p>
<p>So is this</p>
<a href="#" id="test" onClick="jq()">jQuery</a>
jQuery
function jq(){
alert($("p").get(1).innerHTML);
}
id test alert So is this<p>
get eq eq jQuery get dom
$("p").eq(1) jQuery html()$("p").get(1) innerHTML
9. index(obj)
obj (Object):
jQuery
<div id="test1"></div>
<div id="test2"></div>
<a href="#" id="test" onClick="jq()">jQuery</a>
jQuery
function jq(){
alert($("div").index(document.getElementById('test1')));
alert($("div").index(document.getElementById('test2')));
}
id test alert 01
jQuery
<img src="test1.jpg"/>
<img src="test2.jpg"/>
<a href="#" id="test" onClick="jq()">jQuery</a>
jQuery
function jq(){
alert($("img").length);
}
id test alert 2
DOM
1.
herf()
herf(val)
jQuery herf
jQuery
<a href="1.htm" id="test" onClick="jq()">jQuery</a>
jQuery
function jq(){
alert($("#test").href());
$("#test").href("2.html");
}
id test url url 2.html
2.html
jQuery
herf()
(val)
src()
herf(val)
html()
html(val)
id()
val() val(val)
2.
after(html) html
id
(val)
name()
name
append(html) html
<a href="#" id="test" onClick="jq()">jQuery</a>
jQuery
function jq(){
$("#test").append("<b>Hello</b>");
}
appendTo(expr) append(elem)
<p id="test">after</p><a href="#" onClick="jq()">jQuery</a>
jQuery
function jq(){
$("a"). appendTo ($("#test"));
}
clone() jQuery
empty()
<div id="test">
<span>span</span>
<p>after</p>
</div>
<a href="#" onClick="jq()">jQuery</a>
jQuery
function jq(){
$("#test").empty();
}
insertAfter(expr)
insertBefore(expr)
insertAfter(expr)
P
</p>
remove()
empty()empty()remove()
wrap(htm) html
<p>Test Paragraph.</p> <a href="#" onClick="jq()">jQuery</a>
jQuery
function jq(){
$("p").wrap("<div class='wrap'></div>");
}
wrap(elem)
<p>Test Paragraph.</p><div id="content"></div>
<a href="#" onClick="jq()">jQuery</a>
jQuery
function jq(){
$("p").wrap( document.getElementById('content') );
}
3.
add(expr) jquery
<p>Hello</p><p><span>Hello Again</span></p>
<a href="#" onClick="jq()">jQuery</a>
jQuery
function jq(){
var f=$("p").add("span");
for(var i=0;i < $(f).size();i++){
alert($(f).eq(i).html());}
}
$("p")<p>add("span")("p")<span >
3 $("p").add("span") 3
[<p>Hello</p>] [<p><span>Hello
Again</span></p>] [<span>Hello
Again</span>]
add(el) dom
$("p").add(document.getElementById("a"));
add(els) els
<p>Hello</p><p><span>Hello Again</span></p>
jQuery
function jq(){
var f=$("p").add([document.getElementById("a"), document.getElementById("b")])
for(var i=0;i < $(f).size();i++){
alert($(f).eq(i).html());}
}
els [ ]
ancestors () ,
<div>
<p>one</p>
<span>
<u>two</u>
</span>
</div>
jQuery
function jq(){
var f= $("u").ancestors();
for(var i=0;i < $(f).size();i++){
alert($(f).eq(i).html());}
}
<u>[ <u>two</u> ]
<u> div
[<p>one</p><span><u>two</u></span> ]
<body><html>
ancestors (expr) ancestors
var f var f= $("u").ancestors(div),
[ <p>one</p><span><u>two</u></span> ]
children()
<p>one</p>
<div id="ch">
<span>two</span>
</div>
jQuery
function jq(){
alert($("#ch").children().html());
}
$("#ch").children()[ <span>two</span> ]..html()two
children(expr)
<div id="ch">
<span>two</span>
<span id="sp">three</span>
</div>
jQuery
function jq(){
alert($("#ch").children(#sp).html());
}
$("#ch").children()[<span>two</span><span id="sp">three</span> ].
$("#ch").children(#sp)[<span id="sp">three</span> ]
parent () parent (expr) children
contains(str) str
<p>This is just a test.</p><p>So is this</p>
jQuery
function jq(){
alert($("p").contains("test").html());
}
$("p")test$("p").contains("test") [ <p>This
is just a test.</p> ]
end() ,.
filter(expr)
filter(exprs)
exprs [ ]
find(expr)
<p>Hello</p><p id="a">Hello Again</p><p class="selected">And Again</p>
jQuery
function jq(){
alert($("p").find("#a").html())
}
$("p") id a
is(expr) , boolen
<p>Hello</p><p id="a">Hello Again</p><p class="selected">And Again</p>
jQuery
function jq(){
alert($("#a").is("p"));
}
$("#a ") jquery
$("#a").is("div"); ("#a").is("#a")
next() next(expr)
<p>Hello</p><p id="a">Hello Again</p><p class="selected">And Again</p>
jQuery
function jq(){
alert($("p").next().html());
alert($("p").next(".selected").html());
}
$("p").next() [ <p id="a">Hello Again</p> , <p class="selected">And Again</p> ]
<p>one</p><p id="a">two</p>
<a href="#" onclick="js()">jQuery</a>
jQuery
function js(){
alert($("p").not(document.getElementById("a")).html());
alert($("p").not(#a).html());
}
$("p")[<p>one</p> ]
siblings () siblings (expr) jquery
<p>one</p>
<div>
<p id="a">two</p>
</div>
<a href="#" onclick="js()">jQuery</a>
jQuery
function js(){
alert($("div").siblings().eq(1).html());
}
$("div").siblings() [<p>one</p> <a
href="#"
onclick="js()">jQuery</a> ]
alert($("div").siblings(a)[<a href="#" onclick="js()">jQuery</a> ]
4.
addClass(class)
class
removeClass (class)
class
attr (name)
<img src="test.jpg"/><a href="#" onclick="js()">jQuery</a>
jQuery
function js(){
alert($("img").attr("src"));
}
test.jpg
attr (prop)
prop hash
key value
$("img"). removeAttr("alt");
}
<img />
toggleClass (class)
[ <p class="selected">Hello</p>,
<p>Hello Again</p> ]
CSS
javascript css
<div id="a" style="background:blue">css</div>
background document.getElementById("a").style.background
jQuery css
$("#a").background()$("#a").background(red)
$("#a") jQuery [ <div id="a" /div> ]
$("#a").background() background
$("#a").background(red) background redjQuery
css
background
(val)
color()
css(key, value)
left()
left(val)
top(val)
()
color(val)
float()
css(name)
float(val)
overflow()
background
css(prop)
overflow(val)
position()
position(val) top()
JavaScript
$.browser() boolen
$(function(){
if($.browser.msie) {
alert(" IE ");}
else if($.browser.opera) {
alert(" opera ");}
})
msiemozillaoperasafari
$.each(obj, fn) obj fn obj $().each()
$.each( [0,1,2], function(i){ alert( "Item #" + i + ": " + this ); });
012 function(i)
$.each({ name: "John", lang: "JS" }, function(i){ alert( "Name: " + i + ", Value: " + this );
{ name: "John", lang: "JS" } hash hash
$.extend(obj, prop)
var settings = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };
$.extend(settings, options);
settings { validate: true, limit: 5, name: "bar" }
$(function(){
var settings = { validate: false, limit: 5, name: "foo" };
var options = { validate: true, name: "bar" };
$.extend(settings, options);
$.each(settings, function(i){ alert( i + "=" + this ); });
})
$(function(){
var arr = $.merge( [0,1,2], [2,3,4] )
$.each(arr, function(i){ alert(i); });
})
arr [0,1,2,3,4]
$.trim(str)
$.trim(" hello, how are you? ")"hello, how are you?"
javascript getElementById id
style.display
none block, none
setTimeout
height
js jQuery
1 $("#a").toggle("slow"),
, jQuery
jQuery
hide()
toggle(speed)
toggle(speed),
<img src="1.jpg" style="width:150px"/>
<a href="#" onClick='$("img").toggle("slow")'>jQuery</a>
fadeIn(speeds)
fadeOut(speeds)
callback
callback
<img src="1.jpg"/>
<a href="#" onClick='$("img ").fadeIn("slow",function(){ alert("Animation Done."); })'> jQue
ry </a>
,
fadeTo(speed, opacity, callback) speed opacity
callbackOpacity (0-1).
<img src="1.jpg"/><br>
<a href="#" onClick='$("img ").fadeTo("slow",0.55,function(){ alert("Animation Done."); })'>
jQuery </a>
jQuery javascript
slideDown(speeds) 0
<img src="1.jpg" style="display:none"/>
<a href="#" onClick='$("img ").slideDown("slow")'>jQuery</a>
slideDown(speeds,callback) 0
callback
hover(Function, Function)
out function
<style>.red{color:#FF0000}</style>
Html <div id="a">sdf</div>
jQuery
$(function(){
$("#a").hover(function(){$(this).addClass("red");},
function(){ $(this).removeClass("red");
});
})
id a red red
toggle(Function, Function)
<style>.red{color:#FF0000}</style>
Html <div id="a">sdf</div>
jQuery
$(function(){
$("#a"). toggle (function(){$(this).addClass("red");},
function(){ $(this).removeClass("red");
});
})
id a red red
bind(type, fn)
trigger(type)
type $("p").trigger("click")
unbind()
unbind(type)
unbind(type, fn)
Dynamic event(Function)
$("#a").bind("click",function() {
$(this).addClass("red");
})
$("#a").click(function() {
$(this).addClass("red");
});
id a red
jQuery
browers
error(fn)
load(fn)
unload(fn)
form
change(fn)
select(fn)
keyboard
keydown(fn)
keypress(fn)
resize(fn)
scroll(fn)
submit(fn)
keyup(fn)
mouse
click(fn)
dblclick(fn)
mousedown(fn)
mousemove(fn)
mouseout(fn) mouseover(fn)
mouseup(fn)
UI
blur(fn)
focus(fn)
5
click(fn) click() unclick() oneclick(fn) unclick(fn)
click(fn)
click() click
unclick () click
oneclick(fn) click
unclick (fn)
browersformkeyboardmouseUI
Ajax
$.ajax(prop)
key/value
(String)type(get post)
((String)url url
((String)data post
((String)dataType( "xml", "html", "script", "json")
((Boolean)ifModified false
((Number)timeout:$.ajaxTimeout
((Boolean)global ajax true
((Function)error
((Function)success
((Function)complete
jQuery
$.ajax({url: "ajax.htm",
success:function(msg){
$(div"#a").html(msg);
}
});
ajax.htm id a div
$.ajax({ url: "ajax.aspx",
type:"get",
dataType:"html",
data: "name=John&location=Boston",
success:function(msg){
$("#a").html(msg);
}
});
get ajax.aspx id a
$.ajaxTimeout(time)
$.ajaxTimeout( 5000 )
jQuery
http://jquery.com/plugins
http://interface.eyecon.ro/demos
http://www.dyve.net/jquery/
http://bassistance.de/jquery-plugins
google