简介
JQuery是JS库,是对JavaScript的一个封装,也即是说JQ提供了大量的API;于2006年开源,现在已经发展成为集JS、CSS、DOM、Ajax于一体的强大的框架体系。
中文文档:https://www.jquery123.com/
API学习或查询:https://jquery.cuishifeng.cn/
控制页面样式 访问和操作DOM 事件处理 提供了大量的插件 与Ajax技术的封装 提供了大量动画处理
< script type = " text/javascript" src = " jquery-3.5.1.js" > </ script>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script type = " text/javascript" >
console. dir ( $) ;
</ script>
<!DOCTYPE html>
< html>
< head>
< title> SimWor</ title>
< meta charset = " utf-8" >
</ head>
< body>
< div class = " box" > </ div>
</ body>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script type = " text/javascript" >
$ ( ".box" ) . html ( "Hello World!" )
. css ( "color" , "red" )
. hover ( function ( ) {
$ ( this ) . fadeOut ( 2000 ) ;
} ) ;
</ script>
</ html>
<!DOCTYPE html>
< html>
< head>
< title> SimWor</ title>
< meta charset = " utf-8" >
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script type = " text/javascript" >
$ ( document) . ready ( function ( ) {
console. log ( $ ( ".box" ) ) ;
} )
$ ( function ( ) {
console. log ( $ ( ".box" ) ) ;
} )
</ script>
</ head>
< body>
< div class = " box" > </ div>
</ body>
</ html>
选择器
选择器 说明 基本选择器 层次选择器 过滤选择器 表单选择器
基本选择器
操作 说明 id #id class .class element element * 选择所有标签 , 选择多个DOM
<!DOCTYPE html>
< html>
< head>
< title> SimWor</ title>
< meta charset = " utf-8" >
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script type = " text/javascript" >
$ ( function ( ) {
$ ( ".box" ) . css ( {
color: "red" ,
background: "#eee" ,
marginBottom: "10px"
} )
$ ( "#box" ) . css ( {
color: "blue"
} )
$ ( "p" ) . css ( {
color: "green"
} )
console. log ( $ ( "*" ) )
$ ( "#box, .box, p" ) . click ( function ( ) {
console. log ( $ ( this ) . html ( ) ) ;
} )
} )
</ script>
</ head>
< body>
< div class = " box" > box1</ div>
< div class = " box" > box2</ div>
< div class = " box" > box3</ div>
< div id = " box" > box4</ div>
< p> This is a p</ p>
</ body>
</ html>
层次选择器
操作 说明 选择器1 选择器2 空格,选择选择器1的所有后代元素(选择器2) 选择器1>选择器2 大于号,只选择选择器1的子元素 选择器1+选择器2 加号,选择紧挨着选择器1的第一个兄弟元素 选择器1~选择器2 波浪号,选择选择器1的所有兄弟元素
<!DOCTYPE html>
< html>
< head>
< title> SimWor</ title>
< meta charset = " utf-8" >
</ head>
< body>
< div class = " wrapper" >
< span> span1</ span>
< div class = " box" >
< span> span2</ span>
</ div>
</ div>
< p> This is p1</ p>
< p> This is p2</ p>
</ body>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script type = " text/javascript" >
$ ( ".wrapper span" ) . html ( "new span" ) ;
$ ( ".wrapper>span" ) . css ( "color" , "red" ) ;
$ ( ".wrapper+p" ) . css ( "font-size" , "25px" ) ;
$ ( ".wrapper~p" ) . css ( "font-weight" , "bolder" ) ;
</ script>
</ html>
过滤选择器
操作 说明 :first/first() 第一个元素 :last/last() 最后一个元素 :not(selector) 除selector之外的元素 :even 偶数 :odd 奇数 :eq(index) 第n个元素 :gt(index) 大于第n个元素 :lt(index) 小于第n个元素 :header 选择h1-h6所有标题元素
<!DOCTYPE html>
< html>
< head>
< title> SimWor</ title>
< meta charset = " utf-8" >
</ head>
< body>
< ul>
< li> li 1</ li>
< li> li 2</ li>
< li class = " li3" > li 3</ li>
< li> li 4</ li>
< li> li 5</ li>
</ ul>
< h1> h1</ h1>
< h2> h2</ h2>
< h6> h6</ h6>
</ body>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script type = " text/javascript" >
$ ( "ul li:first" ) . css ( "color" , "red" ) ;
$ ( "ul li" ) . last ( ) . css ( "color" , "blue" ) ;
$ ( "ul li:not(.li3)" ) . css ( "font-size" , "25px" ) ;
$ ( "ul li:even" ) . css ( "background-color" , "#eee" ) ;
$ ( "ul li:odd" ) . css ( "background-color" , "pink" ) ;
$ ( "ul>li:eq(1)" ) . css ( "font-weight" , "bold" ) ;
$ ( "ul>li:gt(2)" ) . css ( "margin-top" , "10px" ) ;
$ ( "ul>li:lt(3)" ) . css ( "border" , "3px solid green" ) ;
$ ( ":header" ) . css ( "color" , "green" ) ;
</ script>
</ html>
操作 说明 :contains(text) 获取包含指定文本内容的元素 :empty 获取不包含子元素和文本内容的元素 :parent 获取含有子元素或文本的元素 :has(selector) 获取含有特定选择器的元素
<!DOCTYPE html>
< html>
< head>
< title> SimWor</ title>
< meta charset = " utf-8" >
</ head>
< body>
< p> Today is a good day!</ p>
< p> I am going to swim tomorrow.</ p>
< p> Have a good day~</ p>
< p> </ p>
< p> < span> P has a SPAN</ span> </ p>
</ body>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script type = " text/javascript" >
$ ( "p:contains(day)" ) . css ( "color" , "gray" ) ;
$ ( "p:empty" ) . html ( "Not empty anymore" ) ;
$ ( "p:parent" ) . css ( "background-color" , "#eee" ) ;
$ ( "p:has(span)" ) . css ( "font-weight" , "bold" ) ;
</ script>
</ html>
操作 说明 :hidden 选择display:none或隐藏文本域(hidden) :visible 选择display:block的元素
<!DOCTYPE html>
< html>
< head>
< title> SimWor</ title>
< meta charset = " utf-8" >
</ head>
< body>
< div class = " box1" > box1</ div>
< div class = " box2" > box2</ div>
< p> P</ p>
< input type = " hidden" value = " secret" >
</ body>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script type = " text/javascript" >
$ ( "input:hidden" ) . attr ( "type" , "text" ) ;
$ ( "div:visible" ) . css ( "color" , "red" ) ;
</ script>
</ html>
操作 说明 [attr] 含有特定属性 [attr=value] 含特定属性且值为特定值 [attr!=value] 含特定属性但值不为特定值 [attr^=value] 含特定属性且以特定值开头 [attr$=value] 含特定属性且以特定值结尾 [attr*=value] 含特定属性且值包含特定值 [attr1][attr2][attr3] 同时含有特定多个属性
<!DOCTYPE html>
< html>
< head>
< title> SimWor</ title>
< meta charset = " utf-8" >
</ head>
< body>
< p title = " para" > attr is title, value is para</ p>
< p title = " ParA" > attr is title, valie is not PARA</ p>
< p> attr is empty</ p>
< p id = " para" title = " title" > attr is id</ p>
</ body>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script type = " text/javascript" >
$ ( "p[title]" ) . css ( "color" , "red" ) ;
$ ( "p[title=para]" ) . css ( "font-size" , "25px" ) ;
$ ( "p[title!=para]" ) . css ( "font-weight" , "bold" ) ;
$ ( "p[title^=p]" ) . css ( "background-color" , "#eee" ) ;
$ ( "p[title$=A]" ) . css ( "background-color" , "pink" ) ;
$ ( "p[title*=ar]" ) . css ( "border" , "3px solid green" ) ;
$ ( "p[title][id]" ) . css ( "border" , "3px solid yellow" ) ;
</ script>
</ html>
操作 说明 :input :button :submit :text :password
<!DOCTYPE html>
< html>
< head>
< title> SimWor</ title>
< meta charset = " utf-8" >
</ head>
< body>
< form>
< input type = " text" > < br>
< input type = " password" > < br>
< input type = " button" > < br>
< input type = " submit" > < br>
< button> button</ button>
</ form>
</ body>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script type = " text/javascript" >
$ ( ":input" ) . val ( "general" ) ;
$ ( ":button" ) . css ( "color" , "red" ) ;
$ ( ":submit" ) . css ( "font-size" , "25px" ) ;
$ ( ":password" ) . css ( "background-color" , "pink" ) ;
</ script>
</ html>
DOM操作
属性操作
操作 说明 获取属性 attr(属性名) 设置属性 attr(属性名, 属性值) 删除属性 removeAttr(属性名)
<!DOCTYPE html>
< html>
< head>
< title> SimWor</ title>
< meta charset = " utf-8" >
</ head>
< body>
< p title = " title1" id = " id1" > </ p>
< p title = " title2" id = " id2" > </ p>
</ body>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script type = " text/javascript" >
console. log ( $ ( "p" ) . eq ( 0 ) . attr ( "title" ) ) ;
$ ( "p" ) . eq ( 1 ) . attr ( "title" , "new-title" ) ;
console. log ( $ ( "p" ) . eq ( 1 ) . attr ( "title" ) ) ;
console. log ( $ ( "p" ) . eq ( 0 ) . attr ( "id" ) ) ;
$ ( "p" ) . eq ( 0 ) . removeAttr ( "id" ) ;
console. log ( $ ( "p" ) . eq ( 0 ) . attr ( "id" ) ) ;
</ script>
</ html>
文本内容操作
获取 设置 删除 说明 html() html(内容) html("") 可以操作标签 text() text(内容) text("") 只能操作文本内容,不操作标签 val() val(内容) val("") 只能用于表单组件
<!DOCTYPE html>
< html>
< head>
< title> SimWor</ title>
< meta charset = " utf-8" >
</ head>
< body>
< p> < a href = " #" > < span> This is span</ span> </ a> </ p>
< div> This is div</ div>
< input type = " text" value = " this is input-text" >
</ body>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script type = " text/javascript" >
console. log ( $ ( "p" ) . html ( ) ) ;
console. log ( $ ( "p" ) . text ( ) ) ;
console. log ( $ ( ":text" ) . eq ( 0 ) . val ( ) ) ;
$ ( "p" ) . html ( "<strong>strong</strong>" ) ;
$ ( "div" ) . text ( "<span>span</span>" ) ;
$ ( ":input" ) . eq ( 0 ) . val ( "new-value" ) ;
</ script>
</ html>
元素样式操作
操作 说明 css(“属性名”, “属性值”) 设置一个样式 css({“属性名1”: “属性值1”, “属性名2”: “属性值2”, …}) 设置多个样式 addClass(“类名”) 添加一个类名 addClass(“类名1 类名2 …”) 添加多个类名 removeClass(“类名”) 删除一个类名 removeClass(“类名1 类名2”) 删除多个类名 removeClass() 清空
<!DOCTYPE html>
< html>
< head>
< title> SimWor</ title>
< meta charset = " utf-8" >
< style type = " text/css" >
.background-color {
background-color : gray;
}
.border {
border : 3px solid green;
}
</ style>
</ head>
< body>
< p class = " border" > This is a p</ p>
</ body>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script type = " text/javascript" >
$ ( "p" ) . css ( "color" , "red" ) ;
$ ( "p" ) . css ( { "font-size" : "20px" , "font-weight" : "bold" } ) ;
$ ( "p" ) . addClass ( "background-color" ) ;
$ ( "p" ) . removeClass ( "border" ) ;
</ script>
</ html>
页面元素操作
操作 说明 $(dom节点内容) 创建DOM节点 — — append() 在内部最后添加DOM节点 appendTo() 同时,将DOM节点添加到内部的最后 prepend() 在内部最前添加DOM节点 prependTo() 同上,将DOM节点添加到内部的最前 — — before() 在外部前面添加DOM节点 insertBefore() 同上,将DOM节点添加到外部前面 after() 在外部后面添加DOM节点 insertAfter() 同上,将DOM节点添加到外部后面 — — clone() 复制DOM节点(节点本身) clone(true) 深度克隆,连同DOM节点上绑定的事件 — — remove() 删除当前DOM及其子元素 remove(dom节点) 在同级DOM中删除指定的DOM empty() 删除当前DOM下的子元素,保留当前DOM
<!DOCTYPE html>
< html>
< head>
< title> SimWor</ title>
< meta charset = " utf-8" >
</ head>
< body>
< div class = " box" style =" background-color : #eee" >
< span> 这是已有的内容</ span>
</ div>
< p class = " p1" > This is a p</ p>
< p class = " p2" > This is another p</ p>
</ body>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script type = " text/javascript" >
var dom1 = $ ( "<p><span>这是添加到后面的内容</span></p>" ) ;
var dom2 = $ ( "<p><span>这是添加到前面的内容</span></p>" ) ;
$ ( ".box" ) . append ( dom1) ;
$ ( ".box" ) . prepend ( dom2) ;
$ ( ".box" ) . before ( $ ( "<strong>strong before</strong>" ) ) ;
$ ( ".box" ) . after ( $ ( "<strong>strong after</strong>" ) ) ;
$ ( ".p1, .p2" ) . click ( function ( ) {
$ ( this ) . css ( "color" , "red" ) ;
} )
$ ( ".box" ) . append ( $ ( ".p1" ) . clone ( ) ) ;
$ ( ".box" ) . append ( $ ( ".p2" ) . clone ( true ) ) ;
</ script>
</ html>
小练习:动态删除表格条目
布局
全选
删除
大图预览
<!DOCTYPE html>
< html>
< head>
< title> SimWor</ title>
< meta charset = " utf-8" >
< style>
body {
font-size : 12px;
}
table {
width : 360px;
border-collapse : collapse;
margin : 0 auto;
}
table tr th, td {
border : solid 1px #666;
text-align : center;
}
table tr td img {
border : solid 1px #ccc;
padding : 3px;
width : 42px;
height : 60px;
cursor : hand;
}
table tr td span {
float : left;
padding-left : 12px;
}
table tr th {
background-color : #ccc;
height : 32px;
}
.clsImg {
position : absolute;
border : solid 1px #ccc;
padding : 3px;
width : 85px;
height : 120px;
background-color : #eee;
display : none;
}
.btn {
border : #666 1px solid;
padding : 2px;
width : 50px;
filter : progid : DXImageTransform.Microsoft.Gradient ( GradientType=0, StartColorStr=#ffffff, EndColorStr=#ECE9D8) ;
}
</ style>
</ head>
< body>
< table class = " data" >
< tr>
< th> 选项</ th>
< th> 编号</ th>
< th> 封面</ th>
< th> 购书人</ th>
< th> 性别</ th>
< th> 购书价</ th>
</ tr>
< tr id = " 0" >
< td> < input id = " Checkbox1" type = " checkbox" value = " 0" /> </ td>
< td> 1001</ td>
< td> < img src = " images/01.jpg" alt = " " /> </ td>
< td> 李小明</ td>
< td> 男</ td>
< td> 35.60 元</ td>
</ tr>
< tr id = " 1" >
< td> < input id = " Checkbox2" type = " checkbox" value = " 1" /> </ td>
< td> 1002</ td>
< td> < img src = " images/02.jpg" alt = " " /> </ td>
< td> 刘明明</ td>
< td> 女</ td>
< td> 37.80 元</ td>
</ tr>
< tr id = " 2" >
< td> < input id = " Checkbox3" type = " checkbox" value = " 2" /> </ td>
< td> 1003</ td>
< td> < img src = " images/03.jpg" alt = " " /> </ td>
< td> 张小星</ td>
< td> 女</ td>
< td> 45.60 元</ td>
</ tr>
</ table>
< table>
< tr>
< td style =" text-align : left; height : 28px" >
< span> < input id = " chkAll" type = " checkbox" /> 全选</ span>
< span> < input id = " btnDel" type = " button" value = " 删除" class = " btn" /> </ span>
</ td>
</ tr>
</ table>
< img id = " imgTip" class = " clsImg" src = " images/01.jpg" />
</ body>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script type = " text/javascript" >
$ ( ".data tr:nth-child(even)" ) . css ( "background-color" , "#eee" ) ;
$ ( "#chkAll" ) . click ( function ( ) {
if ( this . checked)
$ ( ".data input[type=checkbox]" ) . attr ( "checked" , true ) ;
else
$ ( ".data input[type=checkbox]" ) . attr ( "checked" , false ) ;
} ) ;
$ ( "#btnDel" ) . click ( function ( ) {
var chAll = $ ( ".data input[type=checkbox]" ) ;
$. each ( chAll, function ( index, item) {
if ( this . checked)
$ ( chAll[ index] ) . parent ( ) . parent ( ) . empty ( ) ;
} ) ;
} ) ;
var x= 5 , y= 15 ;
$ ( ".data img" ) . mousemove ( function ( event) {
$ ( ".clsImg" ) . attr ( "src" , $ ( this ) . attr ( "src" ) )
. show ( )
. css ( {
top: ( event. pageY + y) + "px" ,
left: ( event. pageX + x) + "px"
} ) ;
$ ( ".data img" ) . mouseout ( function ( event) {
$ ( ".clsImg" ) . fadeOut ( ) ;
} ) ;
} ) ;
</ script>
</ html>
事件和动画
元素相关尺寸获取
方法 说明 $(dom).width()/height() 获取元素的宽度/高度 $(dom).innerWidth()/innerHeight() 获取包括padding的宽度/高度 $(dom).outerWidth()/outerHeight() 获取包括padding和border的宽度/高度 $(dom).outerWidth(true)/outerHeight(true) 获取包括padding、border和margin的宽度/高度
方法 说明 $(window).width() $(window).height()
方法 说明 $(document).width() $(document).height()
方法 说明 $(document).scrollTop() $(document).scrollLeft()
<!DOCTYPE html>
< html>
< head>
< title> SimWor</ title>
< meta charset = " utf-8" >
< style type = " text/css" >
* {
margin : 0;
padding : 0;
}
.box {
width : 300px;
height : 300px;
margin : 50px;
padding : 20px;
border : 1px solid red;
}
span {
display : none;
border : 1px solid #000;
padding : 3px;
float : right;
cursor : pointer;
}
</ style>
</ head>
< body>
< div class = " box" > </ div>
< span> 回到顶部</ span>
< div style =" height : 1000px; " > </ div>
</ body>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script type = " text/javascript" >
function log ( info) {
console. log ( info) ;
}
log ( $ ( ".box" ) . width ( ) ) ;
$ ( ".box" ) . width ( "400" ) ;
log ( $ ( ".box" ) . width ( ) ) ;
log ( $ ( ".box" ) . innerWidth ( ) ) ;
log ( $ ( ".box" ) . outerWidth ( ) ) ;
log ( $ ( ".box" ) . outerWidth ( true ) ) ;
log ( $ ( ".box" ) . offset ( ) ) ;
log ( $ ( window) . width ( ) ) ;
log ( $ ( document) . width ( ) ) ;
log ( $ ( document) . scrollTop ( ) ) ;
function topShow ( ) {
var top = Math. round ( $ ( document) . scrollTop ( ) ) ;
if ( top >= 100 )
$ ( "span" ) . fadeIn ( 400 ) ;
else
$ ( "span" ) . fadeOut ( 400 ) ;
}
topShow ( ) ;
$ ( window) . scroll ( function ( ) { topShow ( ) } ) ;
$ ( "span" ) . click ( function ( ) {
$ ( document) . scrollTop ( 0 ) ;
} ) ;
</ script>
</ html>
遍历DOM
$(dom).each(function(index, dom) { … });
<!DOCTYPE html>
< html>
< head>
< title> SimWor</ title>
< meta charset = " utf-8" >
</ head>
< body>
< ul>
< li> </ li>
< li> </ li>
< li> </ li>
< li> </ li>
< li> </ li>
</ ul>
</ body>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script type = " text/javascript" >
$ ( "ul li" ) . each ( function ( index, dom) {
$ ( dom) . html ( "列表项" + ++ index) ;
} ) ;
</ script>
</ html>
事件
事件 说明 click() 鼠标单击 mouseover() 鼠标进入(进入子元素也触发) mouseout() 鼠标离开(离开子元素也触发) mouseenter() 鼠标进入(进入子元素不触发) mouseleave() 鼠标离开(离开子元素不触发) keydown() 按下键盘键 keyup() 松开键盘键 keypress() 按下和松开,即从键盘输入字符时 blur() 元素失去焦点 focus() 元素获得焦点 submit() 用户提交表单时 hover() 同时为mouseenter和mouseleave事件指定处理函数 ready() DOM加载完成时 resize() 浏览器窗口的位置发生变化时 scroll() 滚动条的位置发生变化时
<!DOCTYPE html>
< html>
< head>
< title> SimWor</ title>
< meta charset = " utf-8" >
</ head>
< body>
< div class = " box" > box</ div>
< input type = " text" name = " user" >
< form>
< input type = " text" >
< input type = " submit" >
</ form>
</ body>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script type = " text/javascript" >
$ ( ".son" ) . click ( function ( ) {
$ ( this ) . html ( "clicked" ) ;
} ) . mouseover ( function ( ) {
$ ( this ) . css ( "color" , "red" ) ;
} ) . mouseout ( function ( ) {
$ ( this ) . css ( "color" , "black" ) ;
} ) . on ( "mouseover mouseout" , function ( ) {
console. log ( "Change Color" ) ;
} ) ;
$ ( "input[name=user]" ) . keydown ( function ( event) {
if ( event. keyCode == 13 )
console. log ( $ ( this ) . val ( ) ) ;
} ) ;
$ ( "input[name=user]" ) . focus ( function ( ) {
$ ( this ) . css ( "background-color" , "#eee" ) ;
} ) ;
$ ( "input[name=user]" ) . blur ( function ( ) {
$ ( this ) . css ( "background-color" , "#fff" ) ;
} ) ;
$ ( "form" ) . submit ( function ( ) {
alert ( "Done" ) ;
} ) ;
$ ( ".box" ) . hover ( function ( ) {
$ ( this ) . css ( "font-size" , "28px" ) ;
} , function ( ) {
$ ( this ) . css ( "font-size" , "14px" ) ;
} ) ;
$ ( window) . resize ( function ( ) {
console. log ( "Wow, UR changing" ) ;
} ) ;
</ script>
</ html>
动画
方法 说明 show() 显示 hide() 隐藏 toggle() 切换(显示<—>隐藏) slideDown() 向下滑动 slideUp() 向上滑动 fadeIn() 淡入 fadeOut() 淡出 fadeTo() 调整不透明度 animate() 动画
<!DOCTYPE html>
< html>
< head>
< title> SimWor</ title>
< meta charset = " utf-8" >
</ head>
< body>
< button> 显示</ button>
< button> 隐藏</ button>
< button> 切换</ button> < br>
< div class = " box1" style =" display : none; height : 100px; " > box1</ div>
< button> 向下滑动</ button>
< button> 向上滑动</ button> < br>
< div class = " box2" style =" display : none; height : 100px; " > box2</ div>
< button> 淡入</ button>
< button> 淡出</ button>
< button> 淡出到指定的不透明度</ button> < br>
< div class = " box3" style =" display : none; height : 100px; " > box3</ div>
< button> 演示动画</ button>
< div class = " box4" style =" height : 200px; width : 200px; background-color : pink; " > box4</ div>
</ body>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script type = " text/javascript" >
$ ( "button" ) . eq ( 0 ) . click ( function ( ) {
$ ( ".box1" ) . show ( ) ;
} ) ;
$ ( "button" ) . eq ( 1 ) . click ( function ( ) {
$ ( ".box1" ) . hide ( ) ;
} ) ;
$ ( "button" ) . eq ( 2 ) . click ( function ( ) {
$ ( ".box1" ) . toggle ( ) ;
} ) ;
$ ( "button" ) . eq ( 3 ) . click ( function ( ) {
$ ( ".box2" ) . slideDown ( ) ;
} ) ;
$ ( "button" ) . eq ( 4 ) . click ( function ( ) {
$ ( ".box2" ) . slideUp ( ) ;
} ) ;
$ ( "button" ) . eq ( 5 ) . click ( function ( ) {
$ ( ".box3" ) . fadeIn ( ) ;
} ) ;
$ ( "button" ) . eq ( 6 ) . click ( function ( ) {
$ ( ".box3" ) . fadeOut ( ) ;
} ) ;
$ ( "button" ) . eq ( 7 ) . click ( function ( ) {
$ ( ".box3" ) . fadeTo ( 2000 , .5 ) ;
} ) ;
$ ( "button" ) . eq ( 8 ) . click ( function ( ) {
$ ( ".box4" ) . animate ( {
width: "100px" ,
height: "100px"
} ) ;
} ) ;
</ script>
</ html>
显示/隐藏/切换
滑动
3. 淡入淡出
4. 演示动画
支持以下属性:backgroundPosition、borderWidth、borderBottomWidth、borderLeftWidth、borderRightWidth、borderTopWidth、borderSpacing、margin、marginBottom、marginLeft、marginRight、marginTop、outlineWidth、padding、paddingBottom、paddingLeft、paddingRight、paddingTop、height、width、maxHeight、maxWidth、minHeight、maxWidth、font、fontSize、bottom、left、right、top、letterSpacing、wordSpacing、lineHeight、textIndent
小练习:删除确认框
<!DOCTYPE html>
< html>
< head>
< title> SimWor</ title>
< meta charset = " utf-8" >
< style type = " text/css" >
body {
font-size : 13px;
}
.divShow {
line-height : 32px;
height : 32px;
background-color : #eee;
width : 280px;
padding-left : 10px;
margin : 0 auto;
}
.divShow span {
padding-left : 50px
}
.dialog {
width : 360px;
border : solid 5px #666;
position : absolute;
display : none;
z-index : 101;
}
.dialog .title {
background-color : #fbaf15;
padding : 10px;
color : #fff;
font-weight : bold;
}
.dialog .title img {
float : right;
}
.dialog .content {
background-color : #fff;
padding : 25px;
height : 60px;
}
.dialog .content img {
float : left;
}
.dialog .content span {
float : left;
padding-top : 10px;
padding-left : 10px
}
.dialog .bottom {
text-align : right;
padding : 10px 10px 10px 0px;
background-color : #eee
}
.mask {
width : 100%;
height : 100%;
background-color : #000;
position : absolute;
top : 0px;
left : 0px;
filter : alpha ( opacity=30) ;
display : none;
z-index : 100;
}
.btn {
border : #666 1px solid;
padding : 2px;
width : 65px;
filter : progid : DXImageTransform.Microsoft.Gradient ( GradientType=0, StartColorStr=#ffffff, EndColorStr=#ECE9D8) ;
}
</ style>
</ head>
< body>
< div class = " divShow" >
< input type = " checkbox" id = " checkbox1" >
< a href = " $" > 这是一条可以删除的数据</ a>
< span>
< input type = " button" id = " button1" value = " 删除" class = " btn" >
</ span>
</ div>
< div class = " mask" > </ div>
< div class = " dialog" >
< div class = " title" >
< img class = " close" src = " images/close.gif" alt = " 点击可以关闭" > 删除时提示
</ div>
< div class = " content" >
< img src = " images/delete.jpg" >
< span> 您真的要删除该条记录吗</ span>
</ div>
< div class = " bottom" >
< input type = " button" id = " button3" value = " 确定" class = " btn" >
< input type = " button" id = " button4" value = " 取消" class = " btn" >
</ div>
</ div>
</ body>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script type = " text/javascript" >
function showDialog ( ) {
var objW = $ ( window) ;
var objC = $ ( ".dialog" ) ;
var brsW = objW. width ( ) ;
var brsH = objW. height ( ) ;
var sclL = objW. scrollLeft ( ) ;
var sclT = objW. scrollTop ( ) ;
var curW = objC. width ( ) ;
var curH = objC. height ( ) ;
var left = sclL + ( brsW - curW) / 2 ;
var top = sclT + ( brsH - curH) / 2 ;
objC. css ( {
left: left,
top: top
} )
}
$ ( window) . resize ( function ( ) {
if ( $ ( ".dialog" ) . is ( ":visible" ) )
showDialog ( ) ;
} ) ;
$ ( "#button1" ) . click ( function ( ) {
$ ( ".dialog" ) . show ( ) ;
$ ( ".mask" ) . show ( ) ;
showDialog ( ) ;
} ) ;
$ ( ".close" ) . click ( function ( ) {
$ ( ".dialog" ) . hide ( ) ;
$ ( ".mask" ) . hide ( ) ;
} ) ;
$ ( "#button4" ) . click ( function ( ) {
$ ( ".dialog" ) . hide ( ) ;
$ ( ".mask" ) . hide ( ) ;
} ) ;
$ ( "#button3" ) . click ( function ( ) {
if ( $ ( "input:checked" ) . length != 0 )
$ ( ".divShow" ) . remove ( ) ;
$ ( ".dialog" ) . hide ( ) ;
$ ( ".mask" ) . hide ( ) ;
} ) ;
</ script>
</ html>
JSON、Ajax和跨域
JSON
JSON(JavaScript Object Notation)JS对象简谱是一种轻量级的数据交换格式。
它实际上就是一个对象或数组,或对象中含有数组,或数组中含有对象。
<!DOCTYPE html>
< html>
< head>
< title> SimWor</ title>
< meta charset = " utf-8" >
</ head>
< body>
< table class = " employee" border = " 1" width = " 400" >
< tr>
< th> 员工编号</ th>
< th> 姓名</ th>
< th> 性别</ th>
< th> 年龄</ th>
< th> 家庭住址</ th>
</ tr>
</ table>
< ul class = " score" > </ ul>
< table border = " 1" width = " 800" class = " students" >
< caption> 学生成绩表</ caption>
< thead>
< tr>
< th> 学号</ th>
< th> 姓名</ th>
< th> 性别</ th>
< th> 年龄</ th>
< th> 成绩</ th>
< th> 住址</ th>
</ tr>
</ thead>
</ table>
</ body>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script type = " text/javascript" >
var obj = {
"id" : "928142" ,
"name" : "Tom" ,
"sex" : "male" ,
"age" : 24 ,
"addr" : "GuangZhou China"
} ;
$ ( ".employee" ) . append ( "<tr><td>" + obj. id + "</td>"
+ "<td>" + obj. name + "</td>"
+ "<td>" + obj. sex + "</td>"
+ "<td>" + obj. age + "</td>"
+ "<td>" + obj. addr + "</td></tr>" ) ;
var score = [ 78 , 42 , 63 , 22 , 72 , 93 ] ;
score. forEach ( function ( value, index) {
$ ( ".score" ) . append ( "<li>" + value + "</li>" ) ;
} )
var students = [
{
"id" : "928142" ,
"name" : "Tom" ,
"sex" : "male" ,
"age" : 24 ,
"addr" : "GuangZhou China" ,
"score" : [ 78 , 42 , 63 , 22 , 72 , 93 ]
} ,
{
"id" : "212452" ,
"name" : "Ema" ,
"sex" : "female" ,
"age" : 27 ,
"addr" : "SuZhou China" ,
"score" : [ 26 , 62 , 83 , 22 , 72 , 93 ]
}
] ;
for ( var i in students) {
$ ( ".students" ) . append ( "<tr><td>" + students[ i] . id + "</td>"
+ "<td>" + students[ i] . name + "</td>"
+ "<td>" + students[ i] . sex + "</td>"
+ "<td>" + students[ i] . age + "</td>"
+ "<td>" + students[ i] . score + "</td>"
+ "<td>" + students[ i] . addr + "</td></tr>" ) ;
}
</ script>
</ html>
不需要将数据赋给一个变量 键和字符型值(除数值型、布尔型、null、undefined之外)必须用双引号引起来,不能用单引号或不用引号 不允许出现分号和多余的逗号 不允许出现注释
[
{
"id": "928142",
"name": "Tom",
"sex": "male",
"age": 24,
"addr": "GuangZhou China",
"score": [78,42,63,22,72,93]
},
{
"id": "212452",
"name": "Ema",
"sex": "female",
"age": 27,
"addr": "SuZhou China",
"score": [26,62,83,22,72,93]
}
]
Ajax
Ajax(Asynchronous Javascript And XML)异步的Javascript和XML:目的是让Javascript发送http请求,与后台通信获取数据和信息;
Ajax技术的原理是实例化xmlhttp对象,使用此对象与后台通信;在通信过程中不会影响后续的JS执行,从而实现异步操作。
Ajax可以自己发送请求,不用通过浏览器地址栏,用来实现局部刷新。
$.ajax({
type: "get/post",
url: "",
async: "true/false",
data: {
//上传到服务器端的数据
},
dataType: "json/jsonp/xml/text/javascript", //返回的数据类型或解决跨域(jsonp)
timeout: "" //请求超时时间(毫秒)
}).then(function(result) {
//成功回调的处理代码
}).catch(function() {
//失败回调的处理代码
});
<!DOCTYPE html>
< html>
< head>
< title> SimWor</ title>
< meta charset = " utf-8" >
</ head>
< body>
</ body>
< script src = " https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js" > </ script>
< script type = " text/javascript" >
$. ajax ( {
url: "https://v0.yiketianqi.com/api" ,
data: {
version: "v61" ,
appid: "62532765" ,
appsecret: "PkSo6DqW"
}
} ) . then ( function ( result) {
console. log ( result) ;
} ) . catch ( function ( err) {
console. log ( err) ;
} ) ;
</ script>
</ html>
跨域(JSONP)
同源策略:指的是协议、域名和端口相同;浏览器处于安全方面的考虑,只允许本域名下的接口交互,不同源的客户端脚本,在没有明确授权的情况下,不能读写对方的资源。
前端解决跨域:指定“dataType”为“jsonp”