一 jquery简介

1 jquery是什么 

  • jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team。
  • jQuery是继prototype之后又一个优秀的Javascript框架。其宗旨是——WRITE LESS,DO MORE,写更少的代码,做更多的事情。
  • 它是轻量级的js库(压缩后只有21k) ,这是其它的js库所不及的,它兼容CSS3,还兼容各种浏览器 (IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+)。
  • jQuery是一个快速的,简洁的javaScript库,使用户能更方便地处理HTML documents、events、实现动画效果,并且方便地为网站提供AJAX交互。
  • jQuery还有一个比较大的优势是,它的文档说明很全,而且各种应用也说得很详细,同时还有许多成熟的插件可供选择。
  • jQuery能够使用户的html页保持代码和html内容分离,也就是说,不用再在html里面插入一堆js来调用命令了,只需定义id即可。

2 什么是jQuery对象?

  • jQuery 对象就是通过jQuery包装DOM对象后产生的对象。
  • jQuery 对象是 jQuery 独有的如果一个对象是 jQuery 对象那么它就可以使用 jQuery 里的方法: $(“#test”).html();

比如:

$("#test").html()   意思是指:获取ID为test的元素内的html代码。其中html()是jQuery里的方法

这段代码等同于用DOM实现代码: document.getElementById(" test ").innerHTML;

虽然jQuery对象是包装DOM对象后产生的,但是jQuery无法使用DOM对象的任何方法,同理DOM对象也不能使用jQuery里的方法.乱使用会报错

约定:如果获取的是 jQuery 对象那么要在变量前面加上$. 

var $variable = jQuery 对象

var variable = DOM 对象

基本语法:$(selector).action()

二 寻找元素(重要的选择器和筛选器)

2.1   选择器

2.1.1 基本选择器      $("*")  $("#id")   $(".class")  $("element")  $(".class,p,div")

2.1.2层级选择器       $(".outer div")  $(".outer>div")   $(".outer+div")  $(".outer~div")

2.1.3 基本筛选器      $("li:first")  $("li:eq(2)")  $("li:even") $("li:gt(1)")

2.1.4 属性选择器      $('[id="div1"]')   $('["alex="sb"][id]')

2.1.5 表单选择器      $("[type='text']")----->$(":text")                    注意只适用于input标签

$("input:checked")

2.2 筛选器

2.2.1  过滤筛选器

$("li").eq(2)  $("li").first()  $("ul li").hasclass("test")

2.2.2  查找筛选器

$("div").children(".test")    $("div").find(".test")

$(".test").next()    $(".test").nextAll()   $(".test").nextUntil()

$("div").prev()  $("div").prevAll()  $("div").prevUntil()

$(".test").parent()  $(".test").parents()  $(".test").parentUntil()

$("div").siblings()

实例 左侧菜单 

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>left_menu</title>
  6.  
  7. <script src="js/jquery-2.2.3.js"></script>
  8. <script>
  9. function show(self){
  10. // console.log($(self).text())
  11. $(self).next().removeClass("hide")
  12. $(self).parent().siblings().children(".con").addClass("hide")
  13.  
  14. }
  15. </script>
  16. <style>
  17. .menu{
  18. height: 500px;
  19. width: 30%;
  20. background-color: gainsboro;
  21. float: left;
  22. }
  23. .content{
  24. height: 500px;
  25. width: 70%;
  26. background-color: rebeccapurple;
  27. float: left;
  28. }
  29. .title{
  30. line-height: 50px;
  31. background-color: #425a66;
  32. color: forestgreen;}
  33.  
  34. .hide{
  35. display: none;
  36. }
  37.  
  38. </style>
  39. </head>
  40. <body>
  41.  
  42. <div class="outer">
  43. <div class="menu">
  44. <div class="item">
  45. <div class="title" onclick="show(this);">菜单一</div>
  46. <div class="con">
  47. <div>111</div>
  48. <div>111</div>
  49. <div>111</div>
  50. </div>
  51. </div>
  52. <div class="item">
  53. <div class="title" onclick="show(this);">菜单二</div>
  54. <div class="con hide">
  55. <div>111</div>
  56. <div>111</div>
  57. <div>111</div>
  58. </div>
  59. </div>
  60. <div class="item">
  61. <div class="title" onclick="show(this);">菜单三</div>
  62. <div class="con hide">
  63. <div>111</div>
  64. <div>111</div>
  65. <div>111</div>
  66. </div>
  67. </div>
  68.  
  69. </div>
  70. <div class="content"></div>
  71.  
  72. </div>
  73.  
  74. </body>
  75. </html>

实例 tab切换

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>tab</title>
  6. <script src="js/jquery-2.2.3.js"></script>
  7. <script>
  8. function tab(self){
  9. var index=$(self).attr("xxx")
  10. $("#"+index).removeClass("hide").siblings().addClass("hide")
  11. $(self).addClass("current").siblings().removeClass("current")
  12.  
  13. }
  14. </script>
  15. <style>
  16. *{
  17. margin: 0px;
  18. padding: 0px;
  19. }
  20. .tab_outer{
  21. margin: 0px auto;
  22. width: 60%;
  23. }
  24. .menu{
  25. background-color: #cccccc;
  26. /*border: 1px solid red;*/
  27. line-height: 40px;
  28. }
  29. .menu li{
  30. display: inline-block;
  31. }
  32. .menu a{
  33. border-right: 1px solid red;
  34. padding: 11px;
  35. }
  36. .content{
  37. background-color: tan;
  38. border: 1px solid green;
  39. height: 300px;
  40. }
  41. .hide{
  42. display: none;
  43. }
  44.  
  45. .current{
  46. background-color: darkgray;
  47. color: yellow;
  48. border-top: solid 2px rebeccapurple;
  49. }
  50. </style>
  51. </head>
  52. <body>
  53. <div class="tab_outer">
  54. <ul class="menu">
  55. <li xxx="c1" class="current" onclick="tab(this);">菜单一</li>
  56. <li xxx="c2" onclick="tab(this);">菜单二</li>
  57. <li xxx="c3" onclick="tab(this);">菜单三</li>
  58. </ul>
  59. <div class="content">
  60. <div id="c1">内容一</div>
  61. <div id="c2" class="hide">内容二</div>
  62. <div id="c3" class="hide">内容三</div>
  63. </div>
  64.  
  65. </div>
  66. </body>
  67. </html>

三   操作元素(属性 CSS 和 文档处理)

3.1 属性操作

$("p").text()    $("p").html()   $(":checkbox").val()

$(".test").attr("alex")   $(".test").attr("alex","sb")

$(".test").attr("checked","checked")  $(":checkbox").removeAttr("checked")

$(".test").prop("checked",true)

$(".test").addClass("hide")

实例 编辑框正反选

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="js/jquery-2.2.3.js"></script>
  7. <script>
  8.  
  9. function selectall(){
  10.  
  11. $("table :checkbox").prop("checked",true)
  12. }
  13. function che(){
  14.  
  15. $("table :checkbox").prop("checked",false)
  16. }
  17.  
  18. function reverse(){
  19.  
  20. // var idlist=$("table :checkbox")
  21. // for(var i=0;i<idlist.length;i++){
  22. // var element=idlist[i];
  23. //
  24. // var ischecked=$(element).prop("checked")
  25. // if (ischecked){
  26. // $(element).prop("checked",false)
  27. // }
  28. // else {
  29. // $(element).prop("checked",true)
  30. // }
  31. //
  32. // }
  33.  
  34. $("table :checkbox").each(function(){
  35. if ($(this).prop("checked")){
  36. $(this).prop("checked",false)
  37. }
  38. else {
  39. $(this).prop("checked",true)
  40. }
  41.  
  42. });
  43.  
  44. // li=[10,20,30,40]
  45. //// dic={name:"yuan",sex:"male"}
  46. // $.each(li,function(i,x){
  47. // console.log(i,x)
  48. // })
  49.  
  50. }
  51.  
  52. </script>
  53. </head>
  54. <body>
  55.  
  56. <button onclick="selectall();">全选</button>
  57. <button onclick="che();">取消</button>
  58. <button onclick="reverse();">反选</button>
  59.  
  60. <table border="1">
  61. <tr>
  62. <td><input type="checkbox"></td>
  63. <td>111</td>
  64. </tr>
  65. <tr>
  66. <td><input type="checkbox"></td>
  67. <td>222</td>
  68. </tr>
  69. <tr>
  70. <td><input type="checkbox"></td>
  71. <td>333</td>
  72. </tr>
  73. <tr>
  74. <td><input type="checkbox"></td>
  75. <td>444</td>
  76. </tr>
  77. </table>
  78.  
  79. </body>
  80. </html>

实例 模态对话框

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>批量编辑</title>
  6. <!--<link rel="stylesheet" href="css/mycss.css" />-->
  7. <style>
  8. *{
  9. margin: 0;
  10. padding: 0;
  11. }
  12. body {
  13. font-family: 'Open Sans', sans-serif;
  14. font-weight: 300;
  15. line-height: 1.42em;
  16. color:rebeccapurple;
  17. background-color:goldenrod;
  18. }
  19.  
  20. h1 {
  21. font-size:3em;
  22. font-weight: 300;
  23. line-height:1em;
  24. text-align: center;
  25. color: #4DC3FA;
  26. }
  27. .blue {
  28. color: #185875;
  29. }
  30. .yellow {
  31. color: #FFF842;
  32. }
  33.  
  34. /*!*弹出层罩子*!*/
  35. #full {
  36. background-color:gray;
  37. left:0;
  38. opacity:0.7;
  39. position:absolute;
  40. top:0;
  41. filter:alpha(opacity=30);
  42. }
  43.  
  44. .modified {
  45. background-color: #1F2739;
  46. border:3px solid whitesmoke;
  47. height:400px;
  48. left:50%;
  49. margin:-200px 0 0 -200px;
  50. padding:1px;
  51. position:fixed;
  52. /*position:absolute;*/
  53. top:50%;
  54. width:400px;
  55. display:none;
  56. }
  57. li {
  58. list-style: none;
  59. margin: 20px 0 0 50px;
  60. color: #FB667A;
  61. }
  62. input[type="text"] {
  63. padding: 10px;
  64. border: solid 1px #dcdcdc;
  65. /*transition: box-shadow 3s, border 3s;*/
  66.  
  67. }
  68.  
  69. .iputbutton {
  70. margin: 60px 0 0 50px;
  71. color: whitesmoke;
  72. background-color: #FB667A;
  73. height: 30px;
  74. width: 100px;
  75. border: 1px dashed;
  76.  
  77. }
  78.  
  79. .container th h1 {
  80. font-weight: bold;
  81. font-size: 1em;
  82. text-align: left;
  83. color: #185875;
  84. }
  85.  
  86. .container td {
  87. font-weight: normal;
  88. font-size: 1em;
  89. }
  90.  
  91. .container {
  92.  
  93. width: 80%;
  94. margin: 0 auto;
  95.  
  96. }
  97.  
  98. .container td, .container th {
  99. padding-bottom: 2%;
  100. padding-top: 2%;
  101. padding-left:2%;
  102. }
  103.  
  104. /*单数行*/
  105. .container tr:first-child {
  106. background-color: red;
  107. }
  108.  
  109. /*偶数行*/
  110. .container tr:not(first-child){
  111. background-color: cyan;
  112. }
  113.  
  114. .container th {
  115. background-color: #1F2739;
  116. }
  117.  
  118. .container td:last-child {
  119. color: #FB667A;
  120. }
  121. /*鼠标进过行*/
  122. .container tr:hover {
  123. background-color: #464A52;
  124. }
  125. /*鼠标进过列*/
  126. .container td:hover {
  127. background-color: #FB667A;
  128. color: #403E10;
  129. font-weight: bold;
  130. transform: translate3d(5px, -5px, 0);
  131. }
  132.  
  133. </style>
  134. <script src="jquery-2.2.3.js"></script>
  135. <script>
  136. //点击【编辑】显示
  137.  
  138. $(function () {
  139.  
  140. $("table[name=host] tr:gt(0) td:last-child").click(function (event) {
  141.  
  142. alert("234");
  143. // $("#full").css({height:"100%",width:"100%"});
  144.  
  145. $(".modified").data('current-edit-obj', $(this));
  146.  
  147. $(".modified,#full").show();
  148.  
  149. var hostname = $(this).siblings("td[name=hostname]").text();
  150. $(".modified #hostname").val(hostname);
  151. var ip = $(this).siblings("td[name=ip]").text();
  152. $(".modified #ip").val(ip);
  153. var port = $(this).siblings("td[name=port]").text();
  154. $(".modified #port").val(port);
  155. });
  156. //取消编辑
  157. $(".modified #cancel").bind("click", function () {
  158. $(".modified,#full").hide();
  159. });
  160.  
  161. // 确定修改
  162. $(".modified #ok").bind("click", function (event) {
  163. var check_status = true;
  164. var ths = $(".modified").data('current-edit-obj');
  165. var hostname = $(".modified #hostname").val().trim();
  166. var ip = $(".modified #ip").val().trim();
  167. var port = $(".modified #port").val().trim();
  168. var port = parseInt(port);
  169. console.log(port);
  170. // 端口为空设置为22
  171. if (isNaN(port)) {
  172. alert("您没有设置正常的SSH端口号,将采用默认22号端口");
  173. var port = 22;
  174. }else if ( port > 65535) {
  175. // 如果端口不是一个数字 或者端口大于65535
  176. var check_status = false;
  177. $(".modified #port").css("border-color","red");
  178. alert("端口号超过范围!")
  179. };
  180. // 主机和ip不能是空
  181. if ( hostname == ""){
  182. var check_status = false;
  183. $(".modified #hostname").css("border-color","red");
  184. }else if (ip == "") {
  185. var check_status = false;
  186. $(".modified #ip").css("border-color","red");
  187. };
  188. if (check_status == false){
  189. return false;
  190. }else{
  191. //$(this).css("height","60px") 为什么不用$(this),而用$()
  192. $(ths).siblings("td[name=hostname]").text(hostname);
  193. $(ths).siblings("td[name=ip]").text(ip);
  194. $(ths).siblings("td[name=port]").text(port);
  195. $(".modified,#full").hide();
  196. };
  197.  
  198. });
  199.  
  200. });
  201.  
  202. </script>
  203. </head>
  204. <body>
  205. <h1>
  206. <span class="blue">&lt;</span>Homework1<span class="blue">&gt;</span> <span class="yellow">HostList</span>
  207. </h1>
  208. <div id="full">
  209. <div class="modified">
  210. <li>主机名:<input id="hostname" type="text" value="" />*</li>
  211. <li>ip地址:<input id="ip" type="text" value="" />*</li>
  212. <li>端口号:<input id="port" type="text" value="" />[0-65535]</li>
  213. <div id="useraction">
  214. <input class="iputbutton" type="button" name="确定" id="ok" value="确定"/>
  215. <input class="iputbutton" type="button" name="取消" id="cancel" value="取消"/>
  216. </div>
  217. </div>
  218. </div>
  219. <table class="container" name="host">
  220. <tr>
  221. <th><h1>主机名</h1></th>
  222. <th><h1>IP地址</h1></th>
  223. <th><h1>端口</h1></th>
  224. <th><h1>操作</h1></th>
  225.  
  226. </tr>
  227. <tr>
  228. <td name="hostname">web01</td>
  229. <td name="ip">192.168.2.1</td>
  230. <td name="port">22</td>
  231. <td>编辑 </td>
  232. </tr>
  233. <tr>
  234. <td name="hostname">web02</td>
  235. <td name="ip">192.168.2.2</td>
  236. <td name="port">223</td>
  237. <td>编辑 </td>
  238. </tr>
  239. <tr>
  240. <td name="hostname">web03</td>
  241. <td name="ip">192.168.2.3</td>
  242. <td name="port">232</td>
  243. <td>编辑 </td>
  244. </tr>
  245. <tr>
  246. <td name="hostname">web04</td>
  247. <td name="ip">192.168.2.4</td>
  248. <td name="port">232</td>
  249. <td>编辑 </td>
  250. </tr>
  251. </table>
  252.  
  253. </body>
  254. </html>

3.2 CSS操作

3.2.1(样式)   css("{color:'red',backgroud:'blue'}")

3.2.2(位置)   offset()    position()  scrollTop()  scrollLeft()

3.2.3(尺寸)   height()  width()

实例 返回顶部

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="js/jquery-2.2.3.js"></script>
  7. <script>
  8.  
  9. window.onscroll=function(){
  10.  
  11. var current=$(window).scrollTop();
  12. console.log(current)
  13. if (current>100){
  14.  
  15. $(".returnTop").removeClass("hide")
  16. }
  17. else {
  18. $(".returnTop").addClass("hide")
  19. }
  20. }
  21.  
  22. function returnTop(){
  23. // $(".div1").scrollTop(0);
  24.  
  25. $(window).scrollTop(0)
  26. }
  27.  
  28. </script>
  29. <style>
  30. body{
  31. margin: 0px;
  32. }
  33. .returnTop{
  34. height: 60px;
  35. width: 100px;
  36. background-color: darkgray;
  37. position: fixed;
  38. right: 0;
  39. bottom: 0;
  40. color: greenyellow;
  41. line-height: 60px;
  42. text-align: center;
  43. }
  44. .div1{
  45. background-color: orchid;
  46. font-size: 5px;
  47. overflow: auto;
  48. width: 500px;
  49. }
  50. .div2{
  51. background-color: darkcyan;
  52. }
  53. .div3{
  54. background-color: aqua;
  55. }
  56. .div{
  57. height: 300px;
  58. }
  59. .hide{
  60. display: none;
  61. }
  62. </style>
  63. </head>
  64. <body>
  65. <div class="div1 div">
  66. <p>hello</p>
  67. <p>hello</p>
  68. <p>hello</p>
  69. <p>hello</p>
  70. <p>hello</p>
  71. <p>hello</p>
  72. <p>hello</p>
  73. <p>hello</p>
  74. <p>hello</p>
  75. <p>hello</p>
  76. <p>hello</p>
  77. <p>hello</p>
  78. <p>hello</p>
  79. <p>hello</p>
  80. <p>hello</p>
  81. <p>hello</p>
  82. <p>hello</p>
  83. <p>hello</p>
  84.  
  85. </div>
  86. <div class="div2 div"></div>
  87. <div class="div3 div"></div>
  88. <div class="returnTop hide" onclick="returnTop();">返回顶部</div>
  89. </body>
  90. </html>

实例 滚动菜单 

  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7.  
  8. body{
  9. margin: 0px;
  10. }
  11. img {
  12. border: 0;
  13. }
  14. ul{
  15. padding: 0;
  16. margin: 0;
  17. list-style: none;
  18. }
  19. .clearfix:after {
  20. content: ".";
  21. display: block;
  22. height: 0;
  23. clear: both;
  24. visibility: hidden;
  25. }
  26.  
  27. .wrap{
  28. width: 980px;
  29. margin: 0 auto;
  30. }
  31.  
  32. .pg-header{
  33. background-color: #303a40;
  34. -webkit-box-shadow: 0 2px 5px rgba(0,0,0,.2);
  35. -moz-box-shadow: 0 2px 5px rgba(0,0,0,.2);
  36. box-shadow: 0 2px 5px rgba(0,0,0,.2);
  37. }
  38. .pg-header .logo{
  39. float: left;
  40. padding:5px 10px 5px 0px;
  41. }
  42. .pg-header .logo img{
  43. vertical-align: middle;
  44. width: 110px;
  45. height: 40px;
  46.  
  47. }
  48. .pg-header .nav{
  49. line-height: 50px;
  50. }
  51. .pg-header .nav ul li{
  52. float: left;
  53. }
  54. .pg-header .nav ul li a{
  55. display: block;
  56. color: #ccc;
  57. padding: 0 20px;
  58. text-decoration: none;
  59. font-size: 14px;
  60. }
  61. .pg-header .nav ul li a:hover{
  62. color: #fff;
  63. background-color: #425a66;
  64. }
  65. .pg-body{
  66.  
  67. }
  68. .pg-body .catalog{
  69. position: absolute;
  70. top:60px;
  71. width: 200px;
  72. background-color: #fafafa;
  73. bottom: 0px;
  74. }
  75. .pg-body .catalog.fixed{
  76. position: fixed;
  77. top:10px;
  78. }
  79.  
  80. .pg-body .catalog .catalog-item.active{
  81. color: #fff;
  82. background-color: #425a66;
  83. }
  84.  
  85. .pg-body .content{
  86. position: absolute;
  87. top:60px;
  88. width: 700px;
  89. margin-left: 210px;
  90. background-color: #fafafa;
  91. overflow: auto;
  92. }
  93. .pg-body .content .section{
  94. height: 500px;
  95. }
  96. </style>
  97. </head>
  98. <body>
  99.  
  100. <div class="pg-header">
  101. <div class="wrap clearfix">
  102. <div class="logo">
  103. <a href="#">
  104. <img src="data:images/3.jpg">
  105. </a>
  106. </div>
  107. <div class="nav">
  108. <ul>
  109. <li>
  110. <a href="#">首页</a>
  111. </li>
  112. <li>
  113. <a href="#">功能一</a>
  114. </li>
  115. <li>
  116. <a href="#">功能二</a>
  117. </li>
  118. </ul>
  119. </div>
  120.  
  121. </div>
  122. </div>
  123. <div class="pg-body">
  124. <div class="wrap">
  125. <div class="catalog">
  126. <div class="catalog-item" auto-to="function1"><a>第1张</a></div>
  127. <div class="catalog-item" auto-to="function2"><a>第2张</a></div>
  128. <div class="catalog-item" auto-to="function3"><a>第3张</a></div>
  129. </div>
  130. <div class="content">
  131.  
  132. <div menu="function1" class="section">
  133. <h1>第一章</h1>
  134. </div>
  135. <div menu="function2" class="section">
  136. <h1>第二章</h1>
  137. </div>
  138. <div menu="function3" class="section">
  139. <h1>第三章</h1>
  140. </div>
  141. </div>
  142. </div>
  143. </div>
  144.  
  145. <script type="text/javascript" src="js/jquery-2.2.3.js"></script>
  146. <script type="text/javascript">
  147.  
  148. window.onscroll=function(){
  149. var ws=$(window).scrollTop()
  150. if (ws>50){
  151. $(".catalog").addClass("fixed")
  152. }
  153. else {
  154. $(".catalog").removeClass("fixed")
  155. }
  156. $(".content").children("").each(function(){
  157.  
  158. var offtop=$(this).offset().top;
  159. // console.log(offtop)
  160. var total=$(this).height()+offtop;
  161.  
  162. if (ws>offtop && ws<total){
  163.  
  164. if($(window).height()+$(window).scrollTop()==$(document).height()){
  165. var index=$(".catalog").children(" :last").css("fontSize","40px").siblings().css("fontSize","12px")
  166. console.log(index)
  167. target='div[auto-to="'+index+'"]';
  168. $(".catalog").children(target).css("fontSize","40px").siblings().css("fontSize","12px")
  169.  
  170. }
  171. else{
  172. var index=$(this).attr("menu");
  173. target='div[auto-to="'+index+'"]';
  174. $(".catalog").children(target).css("fontSize","40px").siblings().css("fontSize","12px")
  175. }
  176.  
  177. }
  178.  
  179. })
  180.  
  181. }
  182.  
  183. </script>
  184.  
  185. </body>
  186. </html>

3.3 文档处理

内部插入  $("#c1").append("<b>hello</b>")     $("p").appendTo("div")

prepend()    prependTo()

外部插入  before()  insertBefore()  after insertAfter()

replaceWith()   remove()  empty()  clone()

实例 clone方法的应用

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6.  
  7. </head>
  8. <body>
  9. <div class="outer">
  10. <div class="condition">
  11.  
  12. <div class="icons" style="display:inline-block">
  13. <a onclick="add(this);"><button>+</button></a>
  14. </div>
  15.  
  16. <div class="input" style="display:inline-block">
  17. <input type="checkbox">
  18. <input type="text" value="alex">
  19. </div>
  20. </div>
  21. </div>
  22.  
  23. <script src="js/jquery-2.2.3.js"></script>
  24. <script>
  25.  
  26. function add(self){
  27. var $duplicate = $(self).parent().parent().clone();
  28. $duplicate.find('a[onclick="add(this);"]').attr('onclick',"removed(this)").children("").text("-");
  29. $duplicate.appendTo($(self).parent().parent().parent());
  30.  
  31. }
  32. function removed(self){
  33.  
  34. $(self).parent().parent().remove()
  35.  
  36. }
  37.  
  38. </script>
  39. </body>
  40. </html>

3.4 事件

3.4.1

$(document).ready(function(){}) -----------> $(function(){})

3.4.2

$("p").click(function(){})

$("p").bind("click",function(){})

$("ul").delegate("li","click",function(){})

实例 拖动面板

  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <meta charset="UTF-8">
  5. <title></title>
  6. </head>
  7. <body>
  8. <div style="border: 1px solid #ddd;width: 600px;position: absolute;">
  9. <div id="title" style="background-color: black;height: 40px;color: white;">
  10. 标题
  11. </div>
  12. <div style="height: 300px;">
  13. 内容
  14. </div>
  15. </div>
  16. <script type="text/javascript" src="jquery-2.2.3.js"></script>
  17. <script>
  18. $(function(){
  19. // 页面加载完成之后自动执行
  20. $('#title').mouseover(function(){
  21. $(this).css('cursor','move');
  22. }).mousedown(function(e){
  23. //console.log($(this).offset());
  24. var _event = e || window.event;
  25. // 原始鼠标横纵坐标位置
  26. var ord_x = _event.clientX;
  27. var ord_y = _event.clientY;
  28.  
  29. var parent_left = $(this).parent().offset().left;
  30. var parent_top = $(this).parent().offset().top;
  31.  
  32. $(this).bind('mousemove', function(e){
  33. var _new_event = e || window.event;
  34. var new_x = _new_event.clientX;
  35. var new_y = _new_event.clientY;
  36.  
  37. var x = parent_left + (new_x - ord_x);
  38. var y = parent_top + (new_y - ord_y);
  39.  
  40. $(this).parent().css('left',x+'px');
  41. $(this).parent().css('top',y+'px');
  42.  
  43. })
  44. }).mouseup(function(){
  45. $(this).unbind('mousemove');
  46. });
  47. })
  48. </script>
  49. </body>
  50. </html>

放大镜 

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>bigger</title>
  6. <style>
  7. *{
  8. margin: 0;
  9. padding:0;
  10. }
  11. .outer{
  12. height: 350px;
  13. width: 350px;
  14. border: dashed 5px cornflowerblue;
  15. }
  16. .outer .small_box{
  17. position: relative;
  18. }
  19. .outer .small_box .float{
  20. height: 175px;
  21. width: 175px;
  22. background-color: darkgray;
  23. opacity: 0.4;
  24. fill-opacity: 0.4;
  25. position: absolute;
  26. display: none;
  27.  
  28. }
  29.  
  30. .outer .big_box{
  31. height: 400px;
  32. width: 400px;
  33. overflow: hidden;
  34. position:absolute;
  35. left: 360px;
  36. top: 0px;
  37. z-index: 1;
  38. border: 5px solid rebeccapurple;
  39. display: none;
  40.  
  41. }
  42. .outer .big_box img{
  43. position: absolute;
  44. z-index: 5;
  45. }
  46.  
  47. </style>
  48. </head>
  49. <body>
  50.  
  51. <div class="outer">
  52. <div class="small_box">
  53. <div class="float"></div>
  54. <img src="small.jpg">
  55.  
  56. </div>
  57. <div class="big_box">
  58. <img src="big.jpg">
  59. </div>
  60.  
  61. </div>
  62.  
  63. <script src="js/jquery-2.2.3.js"></script>
  64. <script>
  65.  
  66. $(function(){
  67.  
  68. $(".small_box").mouseover(function(){
  69.  
  70. $(".float").css("display","block");
  71. $(".big_box").css("display","block")
  72.  
  73. })
  74. $(".small_box").mouseout(function(){
  75.  
  76. $(".float").css("display","none");
  77. $(".big_box").css("display","none")
  78.  
  79. })
  80.  
  81. $(".small_box").mousemove(function(e){
  82.  
  83. var _event=e || window.event;
  84.  
  85. var float_width=$(".float").width();
  86. var float_height=$(".float").height();
  87.  
  88. console.log(float_height,float_width);
  89.  
  90. var float_height_half=float_height/2;
  91. var float_width_half=float_width/2;
  92. console.log(float_height_half,float_width_half);
  93.  
  94. var small_box_width=$(".small_box").height();
  95. var small_box_height=$(".small_box").width();
  96.  
  97. // 鼠标点距离左边界的长度与float应该与左边界的距离差半个float的width,height同理
  98. var mouse_left=_event.clientX-float_width_half;
  99. var mouse_top=_event.clientY-float_height_half;
  100.  
  101. if(mouse_left<0){
  102. mouse_left=0
  103. }else if (mouse_left>small_box_width-float_width){
  104. mouse_left=small_box_width-float_width
  105. }
  106.  
  107. if(mouse_top<0){
  108. mouse_top=0
  109. }else if (mouse_top>small_box_height-float_height){
  110. mouse_top=small_box_height-float_height
  111. }
  112.  
  113. $(".float").css("left",mouse_left+"px");
  114. $(".float").css("top",mouse_top+"px")
  115.  
  116. var percentX=($(".big_box img").width()-$(".big_box").width())/ (small_box_width-float_width);
  117. var percentY=($(".big_box img").height()-$(".big_box").height())/(small_box_height-float_height);
  118.  
  119. console.log(percentX,percentY)
  120.  
  121. $(".big_box img").css("left", -percentX*mouse_left+"px")
  122. $(".big_box img").css("top", -percentY*mouse_top+"px")
  123.  
  124. })
  125.  
  126. })
  127.  
  128. </script>
  129. </body>
  130. </html>

3.5 动画效果

实例  隐藏与显示

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="js/jquery-2.2.3.js"></script>
  7. <script>
  8. /**
  9. * Created by yuan on 16/5/5.
  10. */
  11.  
  12. $(document).ready(function(){
  13. $("#hide").click(function(){
  14. $("p").hide(1000);
  15. });
  16. $("#show").click(function(){
  17. $("p").show(1000);
  18. });
  19.  
  20. //用于切换被选元素的 hide() 与 show() 方法。
  21. $("#toggle").click(function(){
  22. $("p").toggle();
  23. })
  24.  
  25. for (var i= 0;i<7;i++){
  26. // 颠倒了常规的$(A).append(B)的操作,即不是把B追加到A中,而是把A追加到B中。
  27. $("<div>").appendTo(document.body);
  28. }
  29. $("div").click(function(){
  30. $(this).hide(2000);
  31. });
  32. });
  33.  
  34. </script>
  35. <link type="text/css" rel="stylesheet" href="style.css">
  36. </head>
  37. <body>
  38. <!--1 隐藏与显示-->
  39. <!--2 淡入淡出-->
  40. <!--3 滑动-->
  41. <!--4 效果-回调:每一个动画执行完毕之后所能执行的函数方法或者所能做的一件事-->
  42.  
  43. <p>hello</p>
  44. <button id="hide">隐藏</button>
  45. <button id="show">显示</button>
  46. <button id="toggle">隐藏/显示</button>
  47.  
  48. </body>
  49. </html>

实例  淡入淡出

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="js/jquery-2.2.3.js"></script>
  7. <script>
  8. $(document).ready(function(){
  9. $("#in").click(function(){
  10. $("#id1").fadeIn(1000);
  11. $("#id2").fadeIn(1000);
  12. $("#id3").fadeIn(1000);
  13.  
  14. });
  15. $("#out").click(function(){
  16. $("#id1").fadeOut(1000);
  17. $("#id2").fadeOut(1000);
  18. $("#id3").fadeOut(1000);
  19.  
  20. });
  21. $("#toggle").click(function(){
  22. $("#id1").fadeToggle(1000);
  23. $("#id2").fadeToggle(1000);
  24. $("#id3").fadeToggle(1000);
  25.  
  26. });
  27. $("#fadeto").click(function(){
  28. $("#id1").fadeTo(1000,0.4);
  29. $("#id2").fadeTo(1000,0.5);
  30. $("#id3").fadeTo(1000,0);
  31.  
  32. });
  33. });
  34.  
  35. </script>
  36.  
  37. </head>
  38. <body>
  39. <button id="in">fadein</button>
  40. <button id="out">fadeout</button>
  41. <button id="toggle">fadetoggle</button>
  42. <button id="fadeto">fadeto</button>
  43.  
  44. <div id="id1" style="display:none; width: 80px;height: 80px;background-color: blueviolet"></div>
  45. <div id="id2" style="display:none; width: 80px;height: 80px;background-color: orangered "></div>
  46. <div id="id3" style="display:none; width: 80px;height: 80px;background-color: darkgreen "></div>
  47.  
  48. </body>
  49. </html>

实例  滑动

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="js/jquery-2.2.3.js"></script>
  7. <script>
  8. $(document).ready(function(){
  9. $("#flipshow").click(function(){
  10. $("#content").slideDown(1000);
  11. });
  12. $("#fliphide").click(function(){
  13. $("#content").slideUp(1000);
  14. });
  15. $("#toggle").click(function(){
  16. $("#content").slideToggle(1000);
  17. })
  18. });
  19. </script>
  20. <style>
  21. #flipshow,#content,#fliphide,#toggle{
  22. padding: 5px;
  23. text-align: center;
  24. background-color: blueviolet;
  25. border:solid 1px red;
  26.  
  27. }
  28. #content{
  29. padding: 50px;
  30. display: none;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35.  
  36. <div id="flipshow">出现</div>
  37. <div id="fliphide">隐藏</div>
  38. <div id="toggle">toggle</div>
  39.  
  40. <div id="content">helloworld</div>
  41.  
  42. </body>
  43. </html>

实例  回调函数

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="js/jquery-2.2.3.js"></script>
  7. <script>
  8.  
  9. $(document).ready(function(){
  10. $("button").click(function(){
  11. $("p").hide(1000,function(){
  12. alert('动画结束')
  13. })
  14.  
  15. // $("p").css('color','red').slideUp(1000).slideDown(2000)
  16. })
  17. });
  18. </script>
  19. </head>
  20. <body>
  21. <button>隐藏</button>
  22. <p>helloworld helloworld helloworld</p>
  23.  
  24. </body>
  25. </html>

3.6 扩展(插件机制)   

  • jquery.extend({})
  • jquery.fn.extend({})

实例 商城菜单

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <meta name="viewport" content="width=device-width">
  6. <meta http-equiv="X-UA-Compatible" content="IE=8">
  7. <title>购物商城</title>
  8. <style>
  9.  
  10. *{
  11. margin: 0;
  12. padding: 0;
  13. }
  14. .hide{
  15. display:none;
  16. }
  17.  
  18. .header-nav {
  19. height: 39px;
  20. background: #c9033b;
  21. }
  22. .header-nav .bg{
  23. background: #c9033b;
  24. }
  25.  
  26. .header-nav .nav-allgoods .menuEvent {
  27. display: block;
  28. height: 39px;
  29. line-height: 39px;
  30. text-decoration: none;
  31. color: #fff;
  32. text-align: center;
  33. font-weight: bold;
  34. font-family: 微软雅黑;
  35. color: #fff;
  36. width: 100px;
  37. }
  38. .header-nav .nav-allgoods .menuEvent .catName {
  39. height: 39px;
  40. line-height: 39px;
  41. font-size: 15px;
  42. }
  43.  
  44. .header-nav .nav-allmenu a {
  45. display: inline-block;
  46. height: 39px;
  47. vertical-align: top;
  48. padding: 0 15px;
  49. text-decoration: none;
  50. color: #fff;
  51. float: left;
  52. }
  53.  
  54. .header-menu a{
  55. color:#656565;
  56. }
  57.  
  58. .header-menu .menu-catagory{
  59. position: absolute;
  60. background-color: #fff;
  61. border-left:1px solid #fff;
  62. height: 316px;
  63. width: 230px;
  64. z-index: 4;
  65. float: left;
  66. }
  67. .header-menu .menu-catagory .catagory {
  68. border-left:4px solid #fff;
  69. height: 104px;
  70. border-bottom: solid 1px #eaeaea;
  71. }
  72. .header-menu .menu-catagory .catagory:hover {
  73. height: 102px;
  74. border-left:4px solid #c9033b;
  75. border-bottom: solid 1px #bcbcbc;
  76. border-top: solid 1px #bcbcbc;
  77. }
  78.  
  79. .header-menu .menu-content .item{
  80. margin-left:230px;
  81. position:absolute;
  82. background-color:white;
  83. height:314px;
  84. width:500px;
  85. z-index:4;
  86. float:left;
  87. border: solid 1px #bcbcbc;
  88. border-left:0;
  89. box-shadow: 1px 1px 5px #999;
  90. }
  91.  
  92. </style>
  93. </head>
  94. <body>
  95.  
  96. <div class="pg-header">
  97.  
  98. <div class="header-nav">
  99. <div class="container narrow bg">
  100. <div class="nav-allgoods left">
  101. <a id="all_menu_catagory" href="#" class="menuEvent">
  102. <strong class="catName">全部商品分类</strong>
  103. <span class="arrow" style="display: inline-block;vertical-align: top;"></span>
  104. </a>
  105. </div>
  106. </div>
  107. </div>
  108. <div class="header-menu">
  109. <div class="container narrow hide">
  110. <div id="nav_all_menu" class="menu-catagory">
  111. <div class="catagory" float-content="one">
  112. <div class="title">家电</div>
  113. <div class="body">
  114. <a href="#">空调</a>
  115. </div>
  116. </div>
  117. <div class="catagory" float-content="two">
  118. <div class="title">床上用品</div>
  119. <div class="body">
  120. <a href="http://www.baidu.com">床单</a>
  121. </div>
  122. </div>
  123. <div class="catagory" float-content="three">
  124. <div class="title">水果</div>
  125. <div class="body">
  126. <a href="#">橘子</a>
  127. </div>
  128. </div>
  129. </div>
  130.  
  131. <div id="nav_all_content" class="menu-content">
  132. <div class="item hide" float-id="one">
  133.  
  134. <dl>
  135. <dt><a href="#" class="red">厨房用品</a></dt>
  136. <dd>
  137. <span>| <a href="#" target="_blank" title="勺子">勺子</a></span>
  138. </dd>
  139. </dl>
  140. <dl>
  141. <dt><a href="#" class="red">厨房用品</a></dt>
  142. <dd>
  143. <span>| <a href="#" target="_blank" title="菜刀">菜刀</a></span>
  144.  
  145. </dd>
  146. </dl>
  147. <dl>
  148. <dt><a href="#" class="red">厨房用品</a></dt>
  149. <dd>
  150. <span>| <a href="#">菜板</a></span>
  151. </dd>
  152. </dl>
  153. <dl>
  154. <dt><a href="#" class="red">厨房用品</a></dt>
  155. <dd>
  156. <span>| <a href="#" target="_blank" title="碗"></a></span>
  157.  
  158. </dd>
  159. </dl>
  160.  
  161. </div>
  162. <div class="item hide" float-id="two">
  163. <dl>
  164. <dt><a href="#" class="red">厨房用品</a></dt>
  165. <dd>
  166. <span>| <a href="#" target="_blank" title="">角阀</a></span>
  167.  
  168. </dd>
  169. </dl>
  170. <dl>
  171. <dt><a href="#" class="red">厨房用品</a></dt>
  172. <dd>
  173. <span>| <a href="#" target="_blank" title="角阀">角阀</a></span>
  174.  
  175. </dd>
  176. </dl>
  177. <dl>
  178. <dt><a href="#" class="red">厨房用品</a></dt>
  179. <dd>
  180. <span>| <a href="#" target="_blank" title="角阀">角阀</a></span>
  181.  
  182. </dd>
  183. </dl>
  184.  
  185. </div>
  186. <div class="item hide" float-id="three">
  187. <dl>
  188. <dt><a href="#" class="red">厨房用品3</a></dt>
  189. <dd>
  190. <span>| <a href="#" target="_blank" title="角阀">角阀3</a></span>
  191.  
  192. </dd>
  193. </dl>
  194. <dl>
  195. <dt><a href="#" class="red">厨房用品3</a></dt>
  196. <dd>
  197. <span>| <a href="http://www.meilele.com/category-jiaofa/" target="_blank" title="角阀">角阀3</a></span>
  198.  
  199. </dd>
  200. </dl>
  201. </div>
  202. </div>
  203. </div>
  204. </div>
  205.  
  206. </div>
  207.  
  208. <script src="js/jquery-2.2.3.js"></script>
  209.  
  210. <script type="text/javascript">
  211. $(document).ready(function () {
  212.  
  213. Change_Menu('#all_menu_catagory','#nav_all_menu', '#nav_all_content');
  214.  
  215. });
  216.  
  217. function Change_Menu(all_menu_catagory,menu, content) {
  218. $all_menu_catagory = $(all_menu_catagory);
  219. $menu = $(menu);
  220. $content = $(content);
  221.  
  222. $all_menu_catagory.bind("mouseover", function () {
  223. $menu.parent().removeClass('hide');
  224. });
  225. $all_menu_catagory.bind("mouseout", function () {
  226. $menu.parent().addClass('hide');
  227. });
  228.  
  229. $menu.children().bind("mouseover", function () {
  230. $menu.parent().removeClass('hide');
  231. $item_content = $content.find('div[float-id="' + $(this).attr("float-content") + '"]');
  232. $item_content.removeClass('hide').siblings().addClass('hide');
  233. });
  234. $menu.bind("mouseout", function () {
  235. $content.children().addClass('hide');
  236. $menu.parent().addClass('hide');
  237. });
  238. $content.children().bind("mouseover", function () {
  239.  
  240. $menu.parent().removeClass('hide');
  241. $(this).removeClass('hide');
  242. });
  243. $content.children().bind("mouseout", function () {
  244.  
  245. $(this).addClass('hide');
  246. $menu.parent().addClass('hide');
  247. });
  248. }
  249. </script>
  250.  
  251. </body>
  252. </html>

实例 编辑框

  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="en">
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. .edit-mode{
  8. background-color: #b3b3b3;
  9. padding: 8px;
  10. text-decoration: none;
  11. color: white;
  12. }
  13. .editing{
  14. background-color: #f0ad4e;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19.  
  20. <div style="padding: 20px">
  21. <input type="button" onclick="CheckAll('#edit_mode', '#tb1');" value="全选" />
  22. <input type="button" onclick="CheckReverse('#edit_mode', '#tb1');" value="反选" />
  23. <input type="button" onclick="CheckCancel('#edit_mode', '#tb1');" value="取消" />
  24.  
  25. <a id="edit_mode" class="edit-mode" href="javascript:void(0);" onclick="EditMode(this, '#tb1');">进入编辑模式</a>
  26.  
  27. </div>
  28. <table border="1">
  29. <thead>
  30. <tr>
  31. <th>选择</th>
  32. <th>主机名</th>
  33. <th>端口</th>
  34. <th>状态</th>
  35. </tr>
  36. </thead>
  37. <tbody id="tb1">
  38. <tr>
  39. <td><input type="checkbox" /></td>
  40. <td edit="true">v1</td>
  41. <td>v11</td>
  42. <td edit="true" edit-type="select" sel-val="1" global-key="STATUS">在线</td>
  43. </tr>
  44. <tr>
  45. <td><input type="checkbox" /></td>
  46. <td edit="true">v1</td>
  47. <td>v11</td>
  48. <td edit="true" edit-type="select" sel-val="2" global-key="STATUS">下线</td>
  49. </tr>
  50. <tr>
  51. <td><input type="checkbox" /></td>
  52. <td edit="true">v1</td>
  53. <td>v11</td>
  54. <td edit="true" edit-type="select" sel-val="1" global-key="STATUS">在线</td>
  55. </tr>
  56. </tbody>
  57. </table>
  58.  
  59. <script type="text/javascript" src="js/jquery-2.2.3.js"></script>
  60. <script>
  61. /*
  62. 监听是否已经按下control键
  63. */
  64. window.globalCtrlKeyPress = false;
  65.  
  66. window.onkeydown = function(event){
  67. if(event && event.keyCode == 17){
  68. window.globalCtrlKeyPress = true;
  69. }
  70. };
  71. window.onkeyup = function(event){
  72. if(event && event.keyCode == 17){
  73. window.globalCtrlKeyPress = false;
  74. }
  75. };
  76.  
  77. /*
  78. 按下Control,联动表格中正在编辑的select
  79. */
  80. function MultiSelect(ths){
  81. if(window.globalCtrlKeyPress){
  82. var index = $(ths).parent().index();
  83. var value = $(ths).val();
  84. $(ths).parent().parent().nextAll().find("td input[type='checkbox']:checked").each(function(){
  85. $(this).parent().parent().children().eq(index).children().val(value);
  86. });
  87. }
  88. }
  89. </script>
  90. <script type="text/javascript">
  91.  
  92. $(function(){
  93. BindSingleCheck('#edit_mode', '#tb1');
  94. });
  95.  
  96. function BindSingleCheck(mode, tb){
  97.  
  98. $(tb).find(':checkbox').bind('click', function(){
  99. var $tr = $(this).parent().parent();
  100. if($(mode).hasClass('editing')){
  101. if($(this).prop('checked')){
  102. RowIntoEdit($tr);
  103. }else{
  104. RowOutEdit($tr);
  105. }
  106. }
  107. });
  108. }
  109.  
  110. function CreateSelect(attrs,csses,option_dict,item_key,item_value,current_val){
  111. var sel= document.createElement('select');
  112. $.each(attrs,function(k,v){
  113. $(sel).attr(k,v);
  114. });
  115. $.each(csses,function(k,v){
  116. $(sel).css(k,v);
  117. });
  118. $.each(option_dict,function(k,v){
  119. var opt1=document.createElement('option');
  120. var sel_text = v[item_value];
  121. var sel_value = v[item_key];
  122.  
  123. if(sel_value==current_val){
  124. $(opt1).text(sel_text).attr('value', sel_value).attr('text', sel_text).attr('selected',true).appendTo($(sel));
  125. }else{
  126. $(opt1).text(sel_text).attr('value', sel_value).attr('text', sel_text).appendTo($(sel));
  127. }
  128. });
  129. return sel;
  130. }
  131.  
  132. STATUS = [
  133. {'id': 1, 'value': "在线"},
  134. {'id': 2, 'value': "下线"}
  135. ];
  136.  
  137. BUSINESS = [
  138. {'id': 1, 'value': "车上会"},
  139. {'id': 2, 'value': "二手车"}
  140. ];
  141.  
  142. function RowIntoEdit($tr){
  143. $tr.children().each(function(){
  144. if($(this).attr('edit') == "true"){
  145. if($(this).attr('edit-type') == "select"){
  146. var select_val = $(this).attr('sel-val');
  147. var global_key = $(this).attr('global-key');
  148. var selelct_tag = CreateSelect({"onchange": "MultiSelect(this);"}, {}, window[global_key], 'id', 'value', select_val);
  149. $(this).html(selelct_tag);
  150. }else{
  151. var orgin_value = $(this).text();
  152. var temp = "<input value='"+ orgin_value+"' />";
  153. $(this).html(temp);
  154. }
  155.  
  156. }
  157. });
  158. }
  159.  
  160. function RowOutEdit($tr){
  161. $tr.children().each(function(){
  162. if($(this).attr('edit') == "true"){
  163. if($(this).attr('edit-type') == "select"){
  164. var new_val = $(this).children(':first').val();
  165. var new_text = $(this).children(':first').find("option[value='"+new_val+"']").text();
  166. $(this).attr('sel-val', new_val);
  167. $(this).text(new_text);
  168. }else{
  169. var orgin_value = $(this).children().first().val();
  170. $(this).text(orgin_value);
  171. }
  172.  
  173. }
  174. });
  175. }
  176.  
  177. function CheckAll(mode, tb){
  178. if($(mode).hasClass('editing')){
  179.  
  180. $(tb).children().each(function(){
  181.  
  182. var tr = $(this);
  183. var check_box = tr.children().first().find(':checkbox');
  184. if(check_box.prop('checked')){
  185.  
  186. }else{
  187. check_box.prop('checked',true);
  188.  
  189. RowIntoEdit(tr);
  190. }
  191. });
  192.  
  193. }else{
  194.  
  195. $(tb).find(':checkbox').prop('checked', true);
  196. }
  197. }
  198.  
  199. function CheckReverse(mode, tb){
  200.  
  201. if($(mode).hasClass('editing')){
  202.  
  203. $(tb).children().each(function(){
  204. var tr = $(this);
  205. var check_box = tr.children().first().find(':checkbox');
  206. if(check_box.prop('checked')){
  207. check_box.prop('checked',false);
  208. RowOutEdit(tr);
  209. }else{
  210. check_box.prop('checked',true);
  211. RowIntoEdit(tr);
  212. }
  213. });
  214.  
  215. }else{
  216. //
  217. $(tb).children().each(function(){
  218. var tr = $(this);
  219. var check_box = tr.children().first().find(':checkbox');
  220. if(check_box.prop('checked')){
  221. check_box.prop('checked',false);
  222. }else{
  223. check_box.prop('checked',true);
  224. }
  225. });
  226. }
  227. }
  228.  
  229. function CheckCancel(mode, tb){
  230. if($(mode).hasClass('editing')){
  231. $(tb).children().each(function(){
  232. var tr = $(this);
  233. var check_box = tr.children().first().find(':checkbox');
  234. if(check_box.prop('checked')){
  235. check_box.prop('checked',false);
  236. RowOutEdit(tr);
  237.  
  238. }else{
  239.  
  240. }
  241. });
  242.  
  243. }else{
  244. $(tb).find(':checkbox').prop('checked', false);
  245. }
  246. }
  247.  
  248. function EditMode(ths, tb){
  249. if($(ths).hasClass('editing')){
  250. $(ths).removeClass('editing');
  251. $(tb).children().each(function(){
  252. var tr = $(this);
  253. var check_box = tr.children().first().find(':checkbox');
  254. if(check_box.prop('checked')){
  255. RowOutEdit(tr);
  256. }else{
  257.  
  258. }
  259. });
  260.  
  261. }else{
  262.  
  263. $(ths).addClass('editing');
  264. $(tb).children().each(function(){
  265. var tr = $(this);
  266. var check_box = tr.children().first().find(':checkbox');
  267. if(check_box.prop('checked')){
  268. RowIntoEdit(tr);
  269. }else{
  270.  
  271. }
  272. });
  273.  
  274. }
  275. }
  276.  
  277. </script>
  278.  
  279. </body>
  280. </html>

Java Script 学习笔记 -- jQuery的更多相关文章

  1. Java Script 学习笔记 -- 基础知识

    Java script 概述 java Script 的简介 JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为 ...

  2. Java Script 学习笔记

    JS编程习惯类: 1. 命名 著名的变量命名规则 只是因为变量名的语法正确,并不意味着就该使用它们.变量还应遵守以下某条著名的命名规则: Camel 标记法 首字母是小写的,接下来的字母都以大写字符开 ...

  3. Java Script 学习笔记 -- Ajax

    AJAX 一 AJAX预备知识:json进阶 1.1 什么是JSON? JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.JSON是用字符串来表示Javas ...

  4. Java Script 学习笔记(一)

    示例如下: JavaScript-警告(alert 消息对话框) 我们在访问网站的时候,有时会突然弹出一个小窗口,上面写着一段提示信息文字.如果你不点击“确定”,就不能对网页做任何操作,这个小窗口就是 ...

  5. Java Script 学习笔记 (二) Casper JS

    1. click() VS mouse.click() 在写自动化脚本要勾选一个复选框时,用casper.mouse.click() 无法选上这个checkbox, 需要用到casper.click( ...

  6. Java Script 学习笔记 (一) 基础

    1. 设置变量 const: 赋常量,不可更改. let :设置可更改变量. ES6 中推荐使用let 而不是var. Let 和var的区别 : let 将变量的作用域限定在当前{}中, var 定 ...

  7. 20145213《Java程序设计学习笔记》第六周学习总结

    20145213<Java程序设计学习笔记>第六周学习总结 说在前面的话 上篇博客中娄老师指出我因为数据结构基础薄弱,才导致对第九章内容浅尝遏止地认知.在这里我还要自我批评一下,其实我事后 ...

  8. [原创]java WEB学习笔记95:Hibernate 目录

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  9. java JDK8 学习笔记——助教学习博客汇总

    java JDK8 学习笔记——助教学习博客汇总 1-6章 (by肖昱) Java学习笔记第一章——Java平台概论 Java学习笔记第二章——从JDK到IDEJava学习笔记第三章——基础语法Jav ...

随机推荐

  1. Nexus4_识别电池真假

    1.参考网址:http://bbs.gfan.com/android-7509786-1-1.html ([6_25更新]给N4换一个“原装的芯”&[免拆]识别真假电池 - LG Nexus ...

  2. 理解WCF(第二部分,部分參考他人)

    該篇的主題:wcf到底是怎工作的? 一.什么是分布式: 首先看一张图: 由上图对比我们可以发现,区别就是前者把服务器放在了一台电脑上,而后者把服务器放在了多台电脑上.这样多台电脑处理起来的速度比一台电 ...

  3. mysql多位小数字段用decimal类型

    转自http://database.51cto.com/art/201005/201651.htm

  4. Java企业微信开发_04_消息推送之发送消息(主动)

    源码请见: Java企业微信开发_00_源码及资源汇总贴 一.本节要点 1.发送消息与被动回复消息 (1)流程不同:发送消息是第三方服务器主动通知微信服务器向用户发消息.而被动回复消息是 用户发送消息 ...

  5. codeforces 632B B. Alice, Bob, Two Teams(暴力)

    B. Alice, Bob, Two Teams time limit per test 1.5 seconds memory limit per test 256 megabytes input s ...

  6. [SHOI2017]期末考试

    题目描述 有n位同学,每位同学都参加了全部的m门课程的期末考试,都在焦急的等待成绩的公布. 第i位同学希望在第ti天或之前得知所有课程的成绩.如果在第ti天,有至少一门课程的成绩没有公布,他就会等待最 ...

  7. noip2017列队(线段树)

    维护一个方阵,支持 1.删掉一个点,剩下的点先向左看齐再向前看齐 2.询问一个位置上是哪个点 $n,m,q \leq 3 \times 10^5$ sol: 我们每行前$m-1$列维护一个线段树,最后 ...

  8. .net Core 2.1 MVC+EF+Redis搭建

    官方学习资料 搭建空MVC框架 1.创建一个空模板 2.创建文件夹 Controllers.Models.Views 3.在Controllers文件夹下创建HomeController.cs 选择C ...

  9. [HDU4652]Dice

    vjudge 题意 \(m\)面骰子,求 1.连续出现\(n\)个相同的停止: 2.连续出现\(n\)个不同的停止 的期望投骰子次数. \(n,m ≤ 10^6\) sol 首先考虑一个转移式子吧. ...

  10. 景深(Depth of Field)

    http://www.cnblogs.com/cxrs/archive/2013/03/22/DepthOfFeild.html 景深(Depth of Field) 什么是景深? 所谓景深,就是当焦 ...