这篇文章主要介绍了jquery调用绑定click事件的3种写法,本文简洁清晰的给出3种写法的代码示例,可以很方便的复制使用,需要的朋友可以参考下
8 {8 V3 }' M7 ^! [1 }第一种方式:
5 e# T- J# j; B8 Q' C$(document).ready(function(){
1 p8 f" B; T* G# M* {$(#clickme).click(function(){
6 {" |( V' ?3 e# k3 balert(hello world click);
: F' e0 H8 l$ N/ M/ X. {});8 r% s* _4 D- F: N) ]* Z _7 ~
第二种方式:6 d+ H; I' w8 U$ c7 R+ X2 I
?
. E% q& k. K( n9 \' L* R; I, ]19 U$ L1 e: W; {- g! V5 W- i, X
2; c4 D. v7 J& x$ l3 _
3
" g9 v R. p& C4 o% c+ ~% [$('#clickmebind').bind(click, function(){. H3 S0 T$ R$ I* c& ~
alert(hello world bind);
$ H% J y/ j) ]});# P) ]/ ^8 \% R
第三种方式:
4 m# j, c5 _8 i2 ^$ b5 i. C$('#clickmeon').on('click', function(){
$ V8 {7 ]6 {$ I7 U, `1 r- Lalert(hello world on);
8 b1 b8 E, J" J5 {$ [9 I* d7 `});. b" J, }) A. ? D0 u8 t
});
5 F2 N- j# f' A6 U注意:第三种方式只适用于jquery 1.7以上的版本* n# Q0 e9 u) j* k/ B
源码如下:
1 Z Q! b& N5 w8 M3 M. O<!doctype html public -//w3c//dtd xhtml 1.0 transitional//en > Z* S8 w' y6 ]9 S
<html xmlns=>; l. t: n$ {. J4 }4 s: c/ X
<head>
, G9 h, y! g7 |. P# b+ Q! S: N9 N<meta http-equiv=content-type content=text/html; charset=gb2312 />: T5 E6 N* e% b0 O; z4 V6 j6 A
<script type=text/javascript src=js/jquery-1.6.1.min.js></script>2 ?2 u2 B# V; ]9 W) @5 g1 T" y/ }& ]
<script type=text/javascript src=js/jquery.validate.js></script>- ?9 ]& }& {6 @, {. C
<script type=text/javascript src=js/jquery.metadata.js></script>
: ^+ S% W' W' }7 w+ K+ h<script type=text/javascript src=js/messages_zh.js></script>9 e9 p: N7 _ z
<style type=text/css>' K. C$ V+ q% p$ j
#frm label.error {
* Z5 w( X0 p ?3 k4 U/ C, X4 ?/ `color: red;/ i' f u2 S. S' H" b
} X) V, e0 c# A/ ?; ~- [# f) X
</style>+ N6 j( ^% ^5 ^' y$ Q3 @3 }$ r
</head>
' Z* k7 d3 e; G3 P. E5 {<script type=text/javascript>1 ?; e, {2 |$ |& D6 h8 S- G9 x# x
$(document).ready(function(){. F- [: }$ {0 y% ~4 A% W
$(#clickme).click(function(){
. L" `9 E: K+ u" `/ T: ialert(hello world click);7 M! U2 u3 W' v$ G% x- m" D: r
});8 b0 I" n4 Q+ h& h& ^* a& |3 |6 y
$('#clickmebind').bind(click, function(){
/ ^/ R& k A* @3 ]# d; Q$ F' a3 oalert(hello world bind);
z* F. b' x1 r2 l, N# O9 F});0 {& i' d7 g1 v( J
$('#clickmeon').on('click', function(){
! M- t3 F2 b( q2 Zalert(hello world on);% @ _) `( U* n( `
});0 x# y. e8 m# x& n! ?' H
});9 I& v) N. {* R5 X' f9 E/ w
</script>
! R! E. }0 @- a/ ?4 ^/ l) P( L<body>
/ k. [( N7 B ^) E4 B<label></label>
, s& }3 ?. ^; W! o! q6 A<form id=frm name=frm method=post action=><label>用 户 名:
! L! ` Q4 J9 e% ^<input type=text name=username id=username />7 R" c8 J# F7 z
</label>
1 F9 E8 B$ u! E( U8 G<p>9 l# _5 q9 z2 ]/ F
<label>邮 编 :<label></label></label> C6 E w: G1 Y4 t
<label>: r0 N+ t3 G0 u
<input type=text name=postcode id=postcode />
& I, h& @. w9 I7 h Y<br />
4 u$ {$ [+ M/ M) i- O, f; Y V</label>& [& H N+ O. {% v
</p>8 b" ]+ Q! H& K: s) x1 X! @3 Y
<p><label>数 字 :/ y. i! z& @, g5 Y- E
<input type=text name=number id=number />/ O+ S9 `* v! P G
</label>
0 {1 S! H" e$ a6 T<br /><label>身份证号:% {7 h0 F8 ^" A/ o9 l
<input type=text name=identifier id=identifier />1 u; b; P2 D" s8 e
</label>5 X6 g$ o0 ~+ P# }7 O
<label>% F4 }$ \3 K1 ^. T6 q* o
<input type=button name=clickme id=clickme value=click me /># ^- P4 T: H% p) g q% y
</label>
) X% r% |- p+ ^; O6 s" f<label>% [' M8 l' C4 }, [) A2 Q
<input type=button name=clickmebind id=clickmebind value=clickme_bind />
3 H2 q- \3 D. U$ W9 W</label>- N4 k" [8 s+ c+ `: t
<label>
) I, B6 Z; _/ E& C5 B( @<input type=button name=clickmeon id=clickmeon value=clickme_on />- I( w* F' t: E( ~$ G1 b; }
</label>2 z1 h6 E# X# {8 r- `& B; k
</p>4 k! ^2 Q- S }2 \0 F0 Z" R
</form>5 P5 I' K; n7 C, C
</body> ( q9 P. ]" k* m
- v8 ~ J. q. Q3 s3 M4 z5 l更多网页制作信息请查看: 网页制作 |
|