HTML与css部分
<!DOCTYPE html>
< html>
< head>
< title> day03</ title>
< script type = " text/javascript" src = " jquery-3.5.1.js" > </ script>
< style type = " text/css" >
* {
margin : 0;
padding : 0;
}
.father {
width : 400px;
height : 400px;
border : 10px solid red;
position : relative;
top : 10px;
left : 10px;
}
.son {
width : 100px;
height : 100px;
border : 10px solid green;
position : absolute;
top : 100px;
left : 100px;
}
#btn8+div {
width : 200px;
height : 200px;
overflow : auto;
}
#btn8+div img {
width : 540px;
height : 360px;
}
#on,
#off {
width : 100px;
height : 100px;
background : red;
}
</ style>
</ head>
< body>
< input type = " button" value = " 获取属性" id = " btn1" >
< input type = " button" value = " 设置属性" id = " btn2" >
< input type = " button" value = " 移除属性" id = " btn3" >
< img src = " ../img/pic13.jpg" alt = " working" title = " todo" id = " test01" >
< br>
< input type = " button" id = " btn4" value = " 是否checked" >
< input type = " checkbox" id = " ckb1" >
< br>
< input type = " button" id = " btn5" value = " 大小设置" >
< br>
< input type = " button" value = " offset()" id = " btn6" >
< input type = " button" value = " position()" id = " btn7" >
< div id = " father" class = " father" >
< div id = " son" class = " son" > </ div>
</ div>
< br>
< input type = " button" value = " 滚动条" id = " btn8" >
< div id = " scroll" >
< img src = " ../img/yh.jpg" >
</ div>
< input type = " button" value = " on简单事件注册" id = " btn9" >
< input type = " button" value = " 添加on委托注册示例" id = " btn10" >
< div id = " on" > on委托注册</ div>
< br>
< input type = " button" value = " 注册" id = " btn11" >
< input type = " button" value = " 解绑" id = " btn12" >
< div id = " off" > </ div>
< input type = " button" value = " 触发已有事件" id = " btn13" >
< input type = " button" value = " 触发自定义事件" id = " btn14" >
< br>
< input type = " button" value = " 事件对象演示" id = " btn15" >
</ body>
</ html>
< script type = " text/javascript" >
$ ( function ( ) {
} )
</ script>
设置、获取、移除属性
$ ( '#btn2' ) . click ( function ( ) {
$ ( '#test01' ) . attr ( "src" , '../img/pic14.jpg' ) ;
$ ( '#test01' ) . attr ( 'aaa' , 'A' ) ;
$ ( '#test01' ) . attr ( 'bbb' , 'B' ) ;
$ ( '#test01' ) . attr ( {
'ccc' : 'C' ,
'ddd' : 'D' ,
} ) ;
} ) ;
$ ( '#btn1' ) . click ( function ( ) {
alert ( $ ( '#test01' ) . attr ( 'src' ) ) ;
} ) ;
$ ( '#btn3' ) . click ( function ( ) {
$ ( '#test01' ) . removeAttr ( 'alt' ) ;
$ ( '#test01' ) . removeAttr ( 'aaa bbb ccc' ) ;
} ) ;
布尔类型属性
$ ( '#btn4' ) . click ( function ( ) {
alert ( $ ( '#ckb1' ) . prop ( 'checked' ) ) ;
} ) ;
位置、尺寸操作
$ ( '#btn5' ) . click ( function ( ) {
$ ( '#test01' ) . width ( 300 ) ;
$ ( '#test01' ) . height ( 300 ) ;
console. log ( $ ( '#test01' ) . width ( ) , $ ( '#test01' ) . height ( ) ) ;
console. log ( $ ( '#test01' ) . innerWidth ( ) , $ ( '#test01' ) . innerHeight ( ) ) ;
console. log ( $ ( '#test01' ) . outerWidth ( ) , $ ( '#test01' ) . outerHeight ( ) ) ;
console. log ( $ ( '#test01' ) . outerWidth ( true ) , $ ( '#test01' ) . outerHeight ( true ) ) ;
console. log ( $ ( window) . width ( ) , $ ( window) . height ( ) ) ;
} ) ;
$ ( '#btn6' ) . click ( function ( ) {
console. log ( $ ( '#father' ) . offset ( ) ) ;
} ) ;
$ ( '#btn7' ) . click ( function ( ) {
console. log ( $ ( '#son' ) . position ( ) ) ;
} ) ;
$ ( '#btn8' ) . click ( function ( ) {
$ ( '#scroll' ) . scrollTop ( 100 ) ;
console. log ( $ ( '#scroll' ) . scrollTop ( ) + ":" + $ ( '#scroll' ) . scrollLeft ( ) ) ;
console. log ( $ ( window) . scrollTop ( ) ) ;
} ) ;
统一事件注册
$ ( '#btn9' ) . on ( 'click' , function ( ) {
alert ( "on简单注册" ) ;
} ) ;
$ ( '#btn10' ) . on ( 'click' , function ( ) {
var $clonediv = $ ( '#on' ) . clone ( ) ;
$ ( '#on' ) . after ( $clonediv) ;
} ) ;
$ ( 'body' ) . on ( 'click' , '#on' , function ( ) {
alert ( '触发点击事件' ) ;
} ) ;
事件解绑
$ ( '#btn11' ) . on ( 'click' , function ( ) {
$ ( '#off' ) . on ( 'click' , function ( ) {
alert ( '单击事件触发' ) ;
} ) ;
} ) ;
$ ( '#btn12' ) . on ( 'click' , function ( ) {
$ ( '#off' ) . off ( 'click' ) ;
} ) ;
事件触发
$ ( '#btn13' ) . on ( 'click' , function ( ) {
$ ( '#off' ) . trigger ( 'click' ) ;
} ) ;
$ ( '#btn14' ) . on ( 'hello' , function ( ) {
alert ( 'Hello Human!' ) ;
} ) ;
$ ( '#btn14' ) . on ( 'click' , function ( ) {
var res = confirm ( 'Hello?' ) ;
if ( res) {
$ ( '#btn14' ) . trigger ( 'hello' ) ;
}
} ) ;
事件对象
$ ( '#btn15' ) . on ( 'click' , function ( e) {
console. log ( e) ;
console. log ( "screen" + e. screenX + ":" + e. screenY) ;
console. log ( "client" + e. clientX + ":" + e. clientY) ;
console. log ( "page" + e. pageX + ":" + e. pageY) ;
e. stopPropagation ( ) ;
e. preventDefaut ( ) ;
return false ;
} ) ;
$ ( document) . on ( 'keydown' , function ( e) {
console. log ( e. keyCode) ;
} )