一.扩展插件
< ! DOCTYPE html>
< html>
< head>
< meta charset= "UTF-8" >
< title> 25_扩展插件< / title>
< style type= "text/css" >
* {
margin : 0px;
}
. div1 {
position : absolute;
width : 100px;
height : 100px;
top : 50px;
left : 10px;
background : red;
}
< / style>
< / head>
< body>
< input type= "checkbox" name= "items" value= "足球" / > 足球
< input type= "checkbox" name= "items" value= "篮球" / > 篮球
< input type= "checkbox" name= "items" value= "羽毛球" / > 羽毛球
< input type= "checkbox" name= "items" value= "乒乓球" / > 乒乓球
< br/ >
< input type= "button" id= "checkedAllBtn" value= "全 选" / >
< input type= "button" id= "checkedNoBtn" value= "全不选" / >
< input type= "button" id= "reverseCheckedBtn" value= "反选" / >
< ! --
1. 扩展jQuery的工具方法
$. extend ( object)
2. 扩展jQuery对象的方法
$. fn. extend ( object)
-- >
< script src= "js/jquery-1.10.1.js" type= "text/javascript" > < / script>
< script type= "text/javascript" src= "js/my_jQuery-plugin.js" > < / script>
< script type= "text/javascript" >
console. log ( $. min ( 3 , 5 ) , $. max ( 3 , 5 ) )
var string = ' my atguigu '
console. log ( '-----' + $. leftTrim ( string) + '-----' )
console. log ( '-----' + $. rightTrim ( string) + '-----' )
var $items = $ ( ':checkbox[name=items]' )
$ ( '#checkedAllBtn' ) . click ( function ( ) {
$items. checkAll ( )
} )
$ ( '#checkedNoBtn' ) . click ( function ( ) {
$items. unCheckAll ( )
} )
$ ( '#reverseCheckedBtn' ) . click ( function ( ) {
$items. reverseCheck ( )
} )
< / script>
< / body>
< / html>
( function ( ) {
$. extend ( {
min : function ( a, b ) {
return a < b ? a : b
} ,
max : function ( a, b ) {
return a > b ? a : b
} ,
leftTrim : function ( str ) {
return str. replace ( / ^\s+ / , '' )
} ,
rightTrim : function ( str ) {
return str. replace ( / \s+$ / , '' )
}
} )
$. fn. extend ( {
checkAll : function ( ) {
this . prop ( 'checked' , true )
} ,
unCheckAll : function ( ) {
this . prop ( 'checked' , false )
} ,
reverseCheck : function ( ) {
this . each ( function ( ) {
this . checked = ! this . checked
} )
}
} )
} ) ( )
二.多库共存
< ! DOCTYPE html>
< html>
< head>
< meta charset= "UTF-8" >
< title> 26_多库共存< / title>
< style type= "text/css" >
* {
margin : 0px;
}
. div1 {
position : absolute;
width : 100px;
height : 100px;
top : 50px;
left : 10px;
background : red;
}
< / style>
< / head>
< body>
< ! --
问题 : 如果有2 个库都有$, 就存在冲突
解决 : jQuery库可以释放$的使用权, 让另一个库可以正常使用, 此时jQuery库只能使用jQuery了
API : jQuery. noConflict ( )
-- >
< script type= "text/javascript" src= "js/myLib.js" > < / script>
< script type= "text/javascript" src= "js/jquery-1.10.1.js" > < / script>
< script type= "text/javascript" >
jQuery. noConflict ( )
$ ( )
jQuery ( function ( ) {
console. log ( '文档加载完成' )
} )
console. log ( '+++++' )
< / script>
< / body>
< / html>
( function ( ) {
window. $ = function ( ) {
console. log ( 'my lib $()...' )
}
} ) ( )
三.onload与ready
< ! DOCTYPE html>
< html lang= "en" >
< head>
< meta charset= "UTF-8" >
< title> window. onload与$ ( document) . ready ( ) < / title>
< / head>
< body>
< h1> 测试window. onload与$ ( document) . ready ( ) < / h1>
< img id= "logo" src= "https://gss0.bdstatic.com/5bVWsj_p_tVS5dKfpU_Y_D3/res/r/image/2017-05-19/6fec71d56242b74eb24b4ac80b817eac.png" >
< ! --
区别 : window. onload与 $ ( document) . ready ( )
* window. onload
* 包括页面的图片加载完后才会回调 ( 晚)
* 只能有一个监听回调
* $ ( document) . ready ( )
* 等同于: $ ( function ( ) { } )
* 页面加载完就回调 ( 早)
* 可以有多个监听回调
-- >
< script type= "text/javascript" src= "js/jquery-1.10.1.js" > < / script>
< script type= "text/javascript" >
console. log ( '直接' , $ ( '#logo' ) . width ( ) )
window. onload = function ( ) {
console. log ( 'onload' , $ ( '#logo' ) . width ( ) )
}
window. onload = function ( ) {
console. log ( 'onload2' , $ ( '#logo' ) . width ( ) )
}
$ ( function ( ) {
console. log ( 'ready' , $ ( '#logo' ) . width ( ) )
} )
$ ( function ( ) {
console. log ( 'ready2' , $ ( '#logo' ) . width ( ) )
} )
$ ( '#logo' ) . on ( 'load' , function ( ) {
console. log ( 'img load' , $ ( this ) . width ( ) )
} )
< / script>
< / body>
< / html>
四.jQueryUI
< ! DOCTYPE html>
< html lang= "en" >
< head>
< meta charset= "UTF-8" >
< title> Title< / title>
< link rel= "stylesheet" href= "jquery-ui.css" >
< / head>
< body>
< ! -- Accordion: 手风琴-- >
< h2> 1. Accordion : 手风琴< / h2>
< div id= "accordion" >
< h3> First< / h3>
< div> Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. < / div>
< h3> Second< / h3>
< div> Phasellus mattis tincidunt nibh. < / div>
< h3> Third< / h3>
< div> Nam dui erat, auctor a, dignissim quis. < / div>
< / div>
< ! -- 2. Autocomplete : 自动搜索匹配 -- >
< h2> Autocomplete< / h2>
< div>
< input id= "autocomplete" title= "type "a"" >
< / div>
< ! -- 3. Tabs : 选项卡 -- >
< h2 > 3. Tabs : 选项卡< / h2>
< div id= "tabs" >
< ul>
< li> < a href= "#tabs-1" > First< / a> < / li>
< li> < a href= "#tabs-2" > Second< / a> < / li>
< li> < a href= "#tabs-3" > Third< / a> < / li>
< / ul>
< div id= "tabs-1" > Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. < / div>
< div id= "tabs-2" > Phasellus mattis tincidunt nibh. Cras orci urna, blandit id, pretium vel, aliquet ornare, felis. Maecenas scelerisque sem non nisl. Fusce sed lorem in enim dictum bibendum. < / div>
< div id= "tabs-3" > Nam dui erat, auctor a, dignissim quis, sollicitudin eu, felis. Pellentesque nisi urna, interdum eget, sagittis et, consequat vestibulum, lacus. Mauris porttitor ullamcorper augue. < / div>
< / div>
< script type= "text/javascript" src= "jquery.js" > < / script>
< script type= "text/javascript" src= "jquery-ui.js" > < / script>
< script type= "text/javascript" >
$ ( '#accordion' ) . accordion ( )
var dataSource = [
"ActionScript" ,
"AppleScript" ,
"Asp" ,
"BASIC" ,
"C" ,
"C++" ,
"Clojure" ,
"COBOL" ,
"ColdFusion" ,
"Erlang" ,
"Fortran" ,
"Groovy" ,
"Haskell" ,
"Java" ,
"JavaScript" ,
"Lisp" ,
"Perl" ,
"PHP" ,
"Python" ,
"Ruby" ,
"Scala" ,
"Scheme"
]
$ ( "#autocomplete" ) . autocomplete ( {
source : dataSource
} ) ;
$ ( '#tabs' ) . tabs ( )
< / script>
< / body>
< / html>
五.laydate
< ! DOCTYPE html>
< html lang= "en" >
< head>
< meta charset= "UTF-8" >
< title> Title< / title>
< script type= "text/javascript" src= "js/laydate.js" > < / script>
< / head>
< body>
< input placeholder= "请输入日期" class = "laydate-icon" onclick= "laydate()" >
< input class = "laydate-icon" id= "demo" value= "2014-6-25更新" >
< script type= "text/javascript" >
; ! function ( ) {
laydate. skin ( 'molv' ) ;
laydate ( {
elem : '#demo'
} )
} ( )
< / script>
< div id= "test1" class = "laydate-icon" > < / div>
< script>
laydate ( {
elem : '#test1' ,
format : 'YYYY/MM' ,
festival : true ,
choose : function ( datas ) {
alert ( '得到:' + datas) ;
}
} ) ;
< / script>
< div id= "hello3" class = "laydate-icon" > < / div>
< script>
laydate ( {
elem : '#hello3' ,
min : laydate. now ( - 1 ) ,
max : laydate. now ( + 1 )
} ) ;
< / script>
< / body>
< / html>
六.表格隔行变色
< ! DOCTYPE html>
< html lang= "en" >
< head>
< meta charset= "UTF-8" >
< title> 01__表格隔行变色< / title>
< style>
div, span, p {
width : 140px;
height : 140px;
margin : 5px;
background : #aaa;
border : #000 1px solid;
float : left;
font- size: 17px;
font- family: Verdana;
}
div. mini {
width : 55px;
height : 55px;
background- color: #aaa;
font- size: 12px;
}
div. hide {
display : none;
}
#data {
width : 600px;
}
#data, td, th {
border- collapse: collapse;
border : 1px solid #aaaaaa;
}
th, td {
height : 28px;
}
#data thead {
background- color: #333399 ;
color : #ffffff;
}
. odd {
background- color: #ccccff;
}
< / style>
< / head>
< body>
< table id= "data" >
< thead>
< tr>
< th> 姓名< / th>
< th> 工资< / th>
< th> 入职时间< / th>
< th> 操作< / th>
< / tr>
< / thead>
< tbody>
< tr>
< td> Tom< / td>
< td> $3500< / td>
< td> 2010 - 10 - 25 < / td>
< td> < a href= "javascript:void(0)" > 删除< / a> < / td>
< / tr>
< tr>
< td> Mary< / td>
< td> $3400< / td>
< td> 2010 - 12 - 1 < / td>
< td> < a href= "javascript:void(0)" > 删除< / a> < / td>
< / tr>
< tr>
< td> King< / td>
< td> $5900< / td>
< td> 2009 - 08 - 17 < / td>
< td> < a href= "javascript:void(0)" > 删除< / a> < / td>
< / tr>
< tr>
< td> Scott< / td>
< td> $3800< / td>
< td> 2012 - 11 - 17 < / td>
< td> < a href= "javascript:void(0)" > 删除< / a> < / td>
< / tr>
< tr>
< td> Smith< / td>
< td> $3100< / td>
< td> 2014 - 01 - 27 < / td>
< td> < a href= "javascript:void(0)" > 删除< / a> < / td>
< / tr>
< tr>
< td> Allen< / td>
< td> $3700< / td>
< td> 2011 - 12 - 05 < / td>
< td> < a href= "javascript:void(0)" > 删除< / a> < / td>
< / tr>
< / tbody>
< / table>
< script type= "text/javascript" src= "jquery-1.10.1.js" > < / script>
< script type= "text/javascript" >
$ ( '#data>tbody>tr:odd' ) . css ( 'background' , '#ccccff' )
< / script>
< / body>
< / html>
七.多Tab点击切换
< ! DOCTYPE html>
< html lang= "en" >
< head>
< meta charset= "UTF-8" >
< title> 02_多Tab点击切换< / title>
< style>
* {
margin : 0 ;
padding : 0 ;
}
#tab li {
float : left;
list- style: none;
width : 80px;
height : 40px;
line- height: 40px;
cursor : pointer;
text- align: center;
}
#container {
position : relative;
}
#content1, #content2, #content3 {
width : 300px;
height : 100px;
padding : 30px;
position : absolute;
top : 40px;
left : 0 ;
}
#tab1, #content1 {
background- color: #ffcc00;
}
#tab2, #content2 {
background- color: #ff00cc;
}
#tab3, #content3 {
background- color: #00ccff;
}
< / style>
< / head>
< body>
< h2> 多Tab点击切换< / h2>
< ul id= "tab" >
< li id= "tab1" value= "1" > 10 元套餐< / li>
< li id= "tab2" value= "2" > 30 元套餐< / li>
< li id= "tab3" value= "3" > 50 元包月< / li>
< / ul>
< div id= "container" >
< div id= "content1" >
10 元套餐详情:< br/ > & nbsp; 每月套餐内拨打100 分钟,超出部分2 毛/ 分钟
< / div>
< div id= "content2" style= "display: none" >
30 元套餐详情:< br/ > & nbsp; 每月套餐内拨打300 分钟,超出部分1.5 毛/ 分钟
< / div>
< div id= "content3" style= "display: none" >
50 元包月详情:< br/ > & nbsp; 每月无限量随心打
< / div>
< / div>
< script type= "text/javascript" src= "jquery-1.10.1.js" > < / script>
< script type= "text/javascript" >
var $contents = $ ( '#container>div' )
var currIndex = 0
$ ( '#tab>li' ) . click ( function ( ) {
$contents[ currIndex] . style. display = 'none'
var index = $ ( this ) . index ( )
$contents[ index] . style. display = 'block'
currIndex = index
} )
< / script>
< / body>
< / html>
八.回到顶部
< ! DOCTYPE html>
< html>
< head>
< meta charset= "UTF-8" >
< title> 03_回到顶部< / title>
< style>
#to_top {
width : 30px;
height : 40px;
font : 14px/ 20px arial;
text- align: center;
background : #06c;
position : fixed;
cursor : pointer;
color : #fff;
left : 1050px;
top : 500px;
}
< / style>
< / head>
< body style= "height: 2000px;" >
< div id= "to_top" > 返回顶部< / div>
< script type= "text/javascript" src= "jquery-1.10.1.js" > < / script>
< script type= "text/javascript" >
$ ( '#to_top' ) . click ( function ( ) {
var $page = $ ( 'html,body' )
var distance = $ ( 'html' ) . scrollTop ( ) + $ ( 'body' ) . scrollTop ( )
var time = 500
var intervalTime = 50
var itemDistance = distance/ ( time/ intervalTime)
var intervalId = setInterval ( function ( ) {
distance -= itemDistance
if ( distance<= 0 ) {
distance = 0
clearInterval ( intervalId)
}
$page. scrollTop ( distance)
} , intervalTime)
} )
< / script>
< / body>
< / html>
九.导航动态显示效果
< ! DOCTYPE html>
< html lang= "en" >
< head>
< meta charset= "UTF-8" >
< title> 04_导航动态显示效果< / title>
< style rel= "stylesheet" >
* {
margin : 0 ;
padding : 0 ;
word- wrap: break - word;
word- break : break - all;
}
body {
background : #FFF ;
color : #333 ;
font : 12px/ 1 . 6em Helvetica, Arial, sans- serif;
}
a {
color : #0287CA;
text- decoration: none;
}
a : hover {
text- decoration: underline;
}
ul, li {
list- style: none;
}
img {
border : none;
}
h1, h2, h3, h4, h5, h6 {
font- size: 1em;
}
html {
overflow : - moz- scrollbars- vertical;
}
#navigation {
width : 784px;
padding : 8px;
margin : 8px auto;
background : #3B5998;
height : 18px;
}
#navigation ul li {
float : left;
margin- right: 14px;
position : relative;
z- index: 100 ;
}
#navigation ul li a {
display : block;
padding : 0 8px;
background : #EEEEEE ;
font- weight: 700 ;
}
#navigation ul li a: hover {
background : none;
color : #fff;
}
#navigation ul li ul {
background- color: #88C366;
position : absolute;
width : 80px;
overflow : hidden;
display : none;
}
#navigation ul li ul li {
border- bottom: 1px solid #BBB ;
text- align: left;
width : 100 % ;
}
< / style>
< / head>
< body>
< div id= "navigation" >
< ul>
< li> < a href= "#" > 首 页< / a> < / li>
< li> < a href= "#" > 衬 衫< / a>
< ul>
< li> < a href= "#" > 短袖衬衫< / a> < / li>
< li> < a href= "#" > 长袖衬衫< / a> < / li>
< li> < a href= "#" > 无袖衬衫< / a> < / li>
< / ul>
< / li>
< li> < a href= "#" > 卫 衣< / a>
< ul>
< li> < a href= "#" > 开襟卫衣< / a> < / li>
< li> < a href= "#" > 套头卫衣< / a> < / li>
< / ul>
< / li>
< li>
< a href= "#" > 裤 子< / a>
< ul>
< li> < a href= "#" > 休闲裤< / a> < / li>
< li> < a href= "#" > 卡其裤< / a> < / li>
< li> < a href= "#" > 牛仔裤< / a> < / li>
< li> < a href= "#" > 短裤< / a> < / li>
< / ul>
< / li>
< li> < a href= "#" > 联系我们< / a> < / li>
< / ul>
< / div>
< script type= "text/javascript" src= "jquery-1.10.1.js" > < / script>
< script type= "text/javascript" >
$ ( '#navigation>ul>li:has(ul)' ) . hover ( function ( ) {
$ ( this ) . children ( 'ul' ) . stop ( true ) . slideDown ( )
} , function ( ) {
$ ( this ) . children ( 'ul' ) . stop ( true ) . slideUp ( )
} )
< / script>
< / body>