1. 事件绑定
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< meta http-equiv = " X-UA-Compatible" content = " IE=edge" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< script src = " ../jQuery.min.js" > </ script>
< style> css">
div {
width : 200px;
height : 200px;
}
.div1 {
background-color : red;
margin-bottom : 20px;
}
.div2 {
background-color : green;
}
</ style>
</ head>
< body>
< div class = " div1" > 1</ div>
< div class = " div2" > 2</ div>
< ul>
< li> 1</ li>
< li> 2</ li>
< li> 3</ li>
< li> 4</ li>
< li> 5</ li>
</ ul>
< ol> </ ol>
< script>
$ ( function ( ) {
$ ( ".div1" ) . click ( function ( ) {
$ ( this ) . css ( 'background' , 'green' ) ;
} ) ;
$ ( ".div2" ) . on ( {
click : function ( ) {
$ ( this ) . css ( 'background' , 'green' ) ;
} ,
mouseover : function ( ) {
$ ( this ) . css ( 'background' , 'yellow' ) ;
} ,
mouseleave : function ( ) {
$ ( this ) . css ( 'background' , 'purple' ) ;
}
} ) ;
$ ( "ul" ) . on ( "click" , "li" , function ( ) {
console. log ( 666 ) ;
} ) ;
$ ( "ol" ) . on ( "click" , "li" , function ( ) {
console. log ( 222 ) ;
} )
var li = $ ( "<li>后来创建的小li</li>" ) ;
$ ( "ol" ) . append ( li) ;
} ) ;
</ script>
</ body>
</ html>
2. 事件解绑
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< meta http-equiv = " X-UA-Compatible" content = " IE=edge" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< script src = " ../jQuery.min.js" > </ script>
< style> css">
div {
width : 200px;
height : 200px;
background-color : red;
}
</ style>
</ head>
< body>
< div> </ div>
< ol>
< li> fs</ li>
< li> sfs</ li>
< li> ksdf </ li>
</ ol>
< p> HelloWorld</ p>
< script>
$ ( function ( ) {
$ ( "div" ) . on ( {
click : function ( ) {
console. log ( '点击' ) ;
} ,
mouseover : function ( ) {
console. log ( '经过' ) ;
}
} ) ;
$ ( "ol" ) . on ( "click" , "li" , function ( ) {
console. log ( 111 ) ;
} )
$ ( "div" ) . off ( "click" ) ;
$ ( "ol" ) . off ( "click" , "li" ) ;
$ ( "p" ) . one ( "click" , function ( ) {
console. log ( '只触发一次!' ) ;
} ) ;
} ) ;
</ script>
</ body>
</ html>
3. 自动触发事件
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< meta http-equiv = " X-UA-Compatible" content = " IE=edge" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< script src = " ../jQuery.min.js" > </ script>
< style> css">
div {
width : 200px;
height : 200px;
background-color : blue;
margin-bottom : 20px;
}
</ style>
</ head>
< body>
< div id = " div0" > </ div>
< div id = " div1" > </ div>
< div id = " div2" > </ div>
< input type = " text" name = " " id = " " >
< script>
$ ( function ( ) {
$ ( "#div0" ) . on ( "click" , function ( ) {
console. log ( 'div0' ) ;
} ) ;
$ ( "#div1" ) . on ( "click" , function ( ) {
console. log ( 'div1' ) ;
} ) ;
$ ( "#div2" ) . on ( "click" , function ( ) {
console. log ( 'div2' ) ;
} ) ;
$ ( "input" ) . on ( "focus" , function ( ) {
console. log ( '自动触发了' ) ;
$ ( this ) . val ( '你好吗' ) ;
} ) ;
$ ( "#div0" ) . click ( ) ;
$ ( "#div1" ) . trigger ( "click" ) ;
$ ( "#div2" ) . triggerHandler ( "click" ) ;
$ ( "input" ) . triggerHandler ( 'focus' ) ;
} )
</ script>
</ body>
</ html>
4. 事件对象
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< meta http-equiv = " X-UA-Compatible" content = " IE=edge" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< title> Document</ title>
< script src = " ../jQuery.min.js" > </ script>
< style> css">
div {
width : 200px;
height : 200px;
background-color : red;
}
</ style>
</ head>
< body>
< div> </ div>
< script>
$ ( function ( ) {
$ ( document) . on ( "click" , function ( ) {
console. log ( '点击了document' ) ;
} ) ;
$ ( "div" ) . on ( "click" , function ( event ) {
console. log ( event) ;
console. log ( '点击了div' ) ;
event. stopPropagation ( ) ;
} ) ;
} ) ;
</ script>
</ body>
</ html>