jQuery-操作元素

3.1 属性操作

$("p").text()   返回文档中标签p的文本内容

$("img").attr("src"); 得到img标签的属性src的数值

$(":checkbox").val() 得到checkbox的数值

$(".test").attr("alex")    获取属性值

$(".test").attr("alex","sb")  设置一个具体的属性值

$(".test").attr("checked","checked")   设置复选框被选中

$(":checkbox").removeAttr("checked")

$("img").removeAttr("src"); 删除img标签的src属性的数值、

$("input[type='checkbox']").prop("disabled", false); 禁用页面上的复选框

$("input[type='checkbox']").prop("checked", true); 选中页面上的所有的复选框

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

$(".test").addClass("hide")针对  class 有test的标签,添加 hide 属性

$("p").removeClass("selected");  针对p标签,删除selected 属性
$("p").toggleClass("selected"); 如果存在,就删除这个属性,如果不存在,就添加这个属性
$('p').html();  获取第一个匹配的html元素
$('p').text(); 返回p标签的文本元素
$("input").val();  获取文本框的数值
 
 
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6     <script src="jquery-3.1.0.js"></script>
 7 </head>
 8 <body>
 9 
10      <button onclick="selectAll();">全选</button>
11      <button onclick="cancel();">取消</button>
12      <button onclick="reverse();">反选</button>
13 
14      <table border="1">
15          <tr>
16              <td><input type="checkbox"></td>
17              <td>111</td>
18          </tr>
19          <tr>
20              <td><input type="checkbox"></td>
21              <td>222</td>
22          </tr>
23          <tr>
24              <td><input type="checkbox"></td>
25              <td>333</td>
26          </tr>
27          <tr>
28              <td><input type="checkbox"></td>
29              <td>444</td>
30          </tr>
31      </table>
32 
33 <script>
34     function selectAll(){
35 
36        // $("[type='checkbox']").prop("checked",true);  //通过type 查找
37         $("table :checkbox").prop("checked",true);  //查找table 下面的checkbox 查找
38         // $("table").find("input").prop("checked",true);  //查找table 下面的checkbox 查找
39 
40     }
41 
42     function cancel(){
43 
44        // $("[type='checkbox']").prop("checked",false);  //通过type 查找
45         $("table :checkbox").prop("checked",false);  //查找table 下面的checkbox 查找
46         // $("table").find("input").prop("checked",false);  //查找table 下面的checkbox 查找
47 
48     };
49 
50     function reverse(){
51 
52       $("table :checkbox").each(function(){ //each 执行一个内部循环。循环内部的function函数
53           if($(this).prop("checked")){ //得到当前操作的标签的checked的值
54               $(this).prop("checked",false) 
55           }
56           else{
57               $(this).prop("checked",true)
58           }
59       })
60 
61     };
62 
63 </script>
64 
65 </body>
66 </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 
 80 
 81 
 82 .container th h1 {
 83     font-weight: bold;
 84     font-size: 1em;
 85       text-align: left;
 86       color: #185875;
 87 }
 88 
 89 .container td {
 90     font-weight: normal;
 91     font-size: 1em;
 92 }
 93 
 94 .container {
 95 
 96     width: 80%;
 97     margin: 0 auto;
 98 
 99 }
100 
101 .container td, .container th {
102     padding-bottom: 2%;
103     padding-top: 2%;
104       padding-left:2%;
105 }
106 
107 /*单数行*/
108 .container tr:first-child {
109     background-color: red;
110 }
111 
112 /*偶数行*/
113 .container tr:not(first-child){
114       background-color: cyan;
115 }
116 
117 .container th {
118     background-color: #1F2739;
119 }
120 
121 .container td:last-child {
122     color: #FB667A;
123 }
124 /*鼠标进过行*/
125 .container tr:hover {
126    background-color: #464A52;
127 }
128 /*鼠标进过列*/
129 .container td:hover {
130   background-color: #FB667A;
131   color: #403E10;
132   font-weight: bold;
133   transform: translate3d(5px, -5px, 0);
134 }
135         </style>
136     </head>
137     <body>
138         <h1>
139             <span class="blue">&lt;</span>Homework1<span class="blue">&gt;</span> <span class="yellow">HostList</span>
140         </h1>
141         <div id="full">
142             <div class="modified">
143                 <li>主机名:<input id="hostname" type="text" value="" />*</li>
144                 <li>ip地址:<input id="ip" type="text" value="" />*</li>
145                 <li>端口号:<input id="port" type="text" value="" />[0-65535]</li>
146                     <div id="useraction">
147                      <input class="iputbutton" type="button" name="确定" id="ok" value="确定"/>
148                     <input class="iputbutton" type="button" name="取消" id="cancel" value="取消"/>
149                     </div>
150             </div>
151         </div>
152         <table class="container" name="host">
153             <tr>
154                 <th><h1>主机名</h1></th>
155                 <th><h1>IP地址</h1></th>
156                 <th><h1>端口</h1></th>
157                 <th><h1>操作</h1></th>
158 
159             </tr>
160             <tr>
161                 <td name="hostname">web01</td>
162                 <td name="ip">192.168.2.1</td>
163                 <td name="port">22</td>
164                 <td>编辑 </td>
165             </tr>
166             <tr>
167                 <td name="hostname">web02</td>
168                 <td name="ip">192.168.2.2</td>
169                 <td name="port">223</td>
170                 <td>编辑 </td>
171             </tr>
172             <tr>
173                 <td name="hostname">web03</td>
174                 <td name="ip">192.168.2.3</td>
175                 <td name="port">232</td>
176                 <td>编辑 </td>
177             </tr>
178             <tr>
179                 <td name="hostname">web04</td>
180                 <td name="ip">192.168.2.4</td>
181                 <td name="port">232</td>
182                 <td>编辑 </td>
183             </tr>
184         </table>
185 
186 
187         <script src="jquery-3.1.0.js"></script>
188         <script>
189             //点击【编辑】显示
190 
191  $(function () {
192 
193 
194     $("table[name=host] tr:gt(0) td:last-child").click(function (event) {
195 
196         alert("234");
197 //        $("#full").css({height:"100%",width:"100%"});
198 
199         $(".modified").data('current-edit-obj', $(this));
200 
201         $(".modified,#full").show();
202 
203         var hostname = $(this).siblings("td[name=hostname]").text();
204         $(".modified #hostname").val(hostname);
205         var ip = $(this).siblings("td[name=ip]").text();
206         $(".modified #ip").val(ip);
207         var port = $(this).siblings("td[name=port]").text();
208         $(".modified #port").val(port);
209     });
210     //取消编辑
211     $(".modified #cancel").bind("click", function () {
212         $(".modified,#full").hide();
213     });
214 
215 //    确定修改
216     $(".modified #ok").bind("click", function (event) {
217         var check_status = true;
218         var ths = $(".modified").data('current-edit-obj');
219         var hostname = $(".modified #hostname").val().trim();
220         var ip = $(".modified #ip").val().trim();
221         var port = $(".modified #port").val().trim();
222         var port = parseInt(port);
223         console.log(port);
224         //        端口为空设置为22
225         if (isNaN(port)) {
226             alert("您没有设置正常的SSH端口号,将采用默认22号端口");
227             var port = 22;
228         }else if ( port > 65535) {
229         //            如果端口不是一个数字 或者端口大于65535
230             var check_status = false;
231             $(".modified #port").css("border-color","red");
232             alert("端口号超过范围!")
233         };
234         // 主机和ip不能是空
235         if ( hostname == ""){
236             var check_status = false;
237             $(".modified #hostname").css("border-color","red");
238         }else if (ip == "") {
239             var check_status = false;
240             $(".modified #ip").css("border-color","red");
241         };
242         if (check_status == false){
243             return false;
244         }else{
245             //$(this).css("height","60px")   为什么不用$(this),而用$()
246             $(ths).siblings("td[name=hostname]").text(hostname);
247             $(ths).siblings("td[name=ip]").text(ip);
248             $(ths).siblings("td[name=port]").text(port);
249             $(".modified,#full").hide();
250         };
251 
252     });
253 
254 });
255 
256     </script>
257     </body>
258 </html>
模态对话框

 

 

3.2 CSS 操作

 3.2.1 样式操作

$("p").css("color"); 获得p标签的颜色

$("p").css({ "color": "#ff0011", "background": "blue" }); 采用字典的方式,将p标签的颜色和背景进行设置

$("p").css("color","red"); 直接设置属性值


3.2.2 位置操作

offset() 标签相对当前视口向上和向左的位置

$("p").last().offset()
Object {top: 50, left: 8}
$("p").last().offset().top

 

position() 相对父标签向上和向左的位置

$("p").position()

 

p.scrollTop() 标签相对滚动条顶部的位置

scrollLeft()  标签相对滚动条左侧的位置

 

3.2.3 尺寸操作

height() 获取当前标签区域的高度

$("p").height(); 获取高度
$("p").height(100); 设置高度


width() 获取当前标签区域的宽度

$("p").width(); 获取宽度
$("p").width(20); 设置宽度
 
 
  1 <!DOCTYPE html>
  2 <html lang="en">
  3 <head>
  4     <meta charset="UTF-8">
  5     <title>Title</title>
  6 
  7 
  8     <style>
  9         body{
 10             margin: 0px;
 11         }
 12         .returnTop{
 13             height: 60px;
 14             width: 100px;
 15             background-color: darkgray;
 16             position: fixed;
 17             right: 0;
 18             bottom: 0;
 19             color: greenyellow;
 20             line-height: 60px;
 21             text-align: center;
 22         }
 23         .div1{
 24             background-color: orchid;
 25             font-size: 5px;
 26             overflow: auto;
 27             width: 500px;
 28         }
 29         .div2{
 30             background-color: darkcyan;
 31         }
 32         .div3{
 33             background-color: aqua;
 34 
 35         }
 36         .div{
 37             height: 400px;
 38         }
 39         .hide{
 40             display: none;
 41         }
 42     </style>
 43 </head>
 44 <body>
 45      <div class="div1 div">
 46          <p>hello</p>
 47          <p>hello</p>
 48          <p>hello</p>
 49          <p>hello</p>
 50          <p>hello</p>
 51          <p>hello</p>
 52          <p>hello</p>
 53          <p>hello</p>
 54          <p>hello</p>
 55          <p>hello</p>
 56          <p>hello</p>
 57          <p>hello</p>
 58          <p>hello</p>
 59          <p>hello</p>
 60          <p>hello</p>
 61          <p>hello</p>
 62          <p>hello</p>
 63          <p>hello</p>
 64             <p>hello</p>
 65             <p>hello</p>
 66 
 67             <p>hello</p>
 68             <p>hello</p>
 69             <p>hello</p>
 70 
 71      </div>
 72      <div class="div2 div"></div>
 73      <div class="div3 div"></div>
 74 
 75      <div class="returnTop hide" onclick="returnTop();">返回顶部</div>
 76 </body>
 77 
 78 <script src="jquery-3.1.0.js"> </script>
 79 
 80 <script>
 81     window.onscroll=function(){      //window是一个对象,表示当前浏览器,onscroll 是监控window的滚动事件
 82 
 83         var current=$(window).scrollTop();//  监测到当前视口,相对滚动条顶部的距离
 84 
 85 
 86         console.log(current);
 87 
 88         if(current >500){
 89             console.log('aaa')
 90             $(".returnTop").removeClass("hide") //当高度大于1000的时候,删除掉hide 属性,显示回到顶部的按钮。
 91         }
 92         else{
 93             $(".returnTop").addClass("hide") //当高度小雨1000 的时候,添加hide 属性。隐藏回到顶部的按钮。
 94         }
 95     }
 96 
 97 
 98 
 99     function  returnTop(){
100         $(window).scrollTop(0) //将滚动条的相对高度设置为0,回到页面顶端
101         $('.div1').scrollTop(0)//将div1的部分,滚动条也回到顶部
102     }
103 
104 
105 
106 </script>
107 </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 
 97         .show{
 98             font-size:20px;
 99             background-color:blueviolet;
100         }
101     </style>
102 </head>
103 <body>
104 
105     <div class="pg-header">
106         <div class="wrap clearfix">
107             <div class="logo">
108            顶部
109             </div>
110             <div class="nav">
111                 <ul>
112                     <li>
113                         <a  href="#">首页</a>
114                     </li>
115                     <li>
116                         <a  href="#">功能一</a>
117                     </li>
118                     <li>
119                         <a  href="#">功能二</a>
120                     </li>
121                 </ul>
122             </div>
123 
124         </div>
125     </div>
126     <div class="pg-body">
127         <div class="wrap">
128             <div class="catalog">
129                 <div class="catalog-item" auto-to="function1"><a>第1张</a></div>
130                 <div class="catalog-item" auto-to="function2"><a>第2张</a></div>
131                 <div class="catalog-item" auto-to="function3"><a>第3张</a></div>
132             </div>
133             <div class="content">
134 
135                 <div menu="function1" class="section">
136                     <h1>第一章</h1>
137                 </div>
138                 <div menu="function2" class="section">
139                     <h1>第二章</h1>
140                 </div>
141                 <div menu="function3" class="section">
142                     <h1>第三章</h1>
143                 </div>
144             </div>
145         </div>
146     </div>
147 
148     <script type="text/javascript" src="jquery-3.1.0.js"></script>
149     <script type="text/javascript">
150 
151 
152 
153         window.onscroll=function(){
154             var roll=$(window).scrollTop()
155 
156             if(roll >50){ //头部高度为50
157                 $(".catalog").addClass("fixed")
158             }
159             else{
160                 $(".catalog").removeClass("fixed")
161             }
162 
163 
164             $(".content").children().each(function(){
165                                               var offtop = $(this).offset().top;
166 
167                                               var height=$(this).height();
168 
169                                               var total = offtop + height;
170 
171 
172                                               if(roll > offtop &&roll <total){
173 
174 
175                                                   if(roll +$(window).height() == $(document).height()){ //如果滑动到最后,文档高度==滚动 高度+ 视口高度
176                                                       $(".catalog").children().last().addClass("show")
177                                                       $(".catalog").children().last().siblings().removeClass("show")
178 
179                                                   }
180 
181                                                   else{
182                                                       var index = $(this).attr("menu") //得到当前滑动到的块(标签的)的menu的属性的值
183                                                   var target="div[auto-to="+index+']'// 查找在左侧catelog中和右侧menu对应的标签  $("div[auto='aaaaa']") 。 $("input[name='dddddd']")
184                                                   $('.catalog').children(target).addClass('show');
185                                                   $(".catalog").children(target).siblings().removeClass("show");
186 
187 
188                                                   }
189 
190                                               }
191 
192 
193 
194 
195                                           }
196 
197 
198             )
199 
200         }
201 
202 //
203 //
204 //          var offtop=$(this).offset().top;
205 ////             console.log(offtop)
206 //             var total=$(this).height()+offtop;
207 //
208 //             if (ws>offtop && ws<total){
209 //
210 //                 if($(window).height()+$(window).scrollTop()==$(document).height()){
211 //                     var index=$(".catalog").children(" :last").css("fontSize","40px").siblings().css("fontSize","12px")
212 //                     console.log(index)
213 //                 target='div[auto-to="'+index+'"]';
214 //                 $(".catalog").children(target).css("fontSize","40px").siblings().css("fontSize","12px")
215 //
216 //                 }
217 //                 else{
218 //                      var index=$(this).attr("menu");
219 //                 target='div[auto-to="'+index+'"]';
220 //                 $(".catalog").children(target).css("fontSize","40px").siblings().css("fontSize","12px")
221 //                 }
222 //
223 //
224 //             }
225 //
226 //         })
227 
228 
229 
230     </script>
231 
232 
233 </body>
234 </html>
roll 滚动菜单

 

 

 

3.3 文档操作

 内部插入     向每个匹配的元素内部追加内容

append(content|fn)  

appendTo(content)

prepend(content)

prependTo(content)

 

外部插入  在每个匹配的元素平级别插入内容

after

before

insertAfter

insertBefore

replaceWith

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>
colne的样例

 

 

3.5 事件

     事件的绑定,可以在标签上绑定。也可以在jQuery使用的时候绑定,例如$("p").click(xxxx) 等价于  <p  onlick = "xxx">

      3.5.1

               $(document).ready(function(){}) -----------> $(function(){})  页面加载后自动执行

      3.5.2

               $("p").click(function(){})   绑定一个事件

               $("p").bind("click",function(){})     向匹配元素添加一个或者多个事件               

               $("ul").delegate("li","click",function(){})     delegate  事件委托。点击标签的时候,在事件绑定。适用于未来数据

  

 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         }
 43         .outer .big_box img{
 44             position: absolute;
 45             z-index: 5;
 46         }
 47 
 48 
 49     </style>
 50 </head>
 51 <body>
 52 
 53 
 54         <div class="outer">
 55             <div class="small_box">
 56                 <div class="float"></div>
 57                 <img src="small.jpg">
 58 
 59             </div>
 60             <div class="big_box">
 61                 <img src="big.jpg">
 62             </div>
 63 
 64         </div>
 65 
 66 
 67 <script src="jquery-3.1.0.js"></script>
 68 <script>
 69 
 70 
 71     $(function(){
 72 
 73 
 74         // 鼠标悬浮,显示
 75         $(".small_box").mouseover(function(){
 76             $(".float").css("display","block");
 77             $(".big_box").css("display","block");
 78         })
 79 
 80         //鼠标移出,隐藏
 81         $(".small_box").mouseout(function(){
 82             $(".float").css("display","none");
 83             $(".big_box").css("display","none");
 84         })
 85 
 86         //鼠标移动,跟随显示
 87         $('.small_box').mousemove(function(e){
 88             var _event = e || window.event;
 89 
 90             var float_width = $(".float").width();
 91             var float_height = $(".float").height();
 92 
 93             var small_box_width = $(".small_box").height();
 94             var small_box_height = $(".small_box").width();
 95 
 96             var mouse_left = event.clientX - float_width/2;
 97             var mouse_top = event.clientY-float_height/2;
 98 
 99             if (mouse_left <0)(mouse_left =0)
100             else if(mouse_left > small_box_width - float_width){
101                 mouse_left =  small_box_width - float_width
102             }
103 
104             if (mouse_top <0)(mouse_top =0)
105             else if(mouse_top > small_box_height - float_height){
106                 mouse_left =  small_box_height - float_height
107             }
108 
109 
110             $('.float').css("left",mouse_left + "px");
111             $('.float').css("top",mouse_top + "px");
112 
113 
114             var percentX = ($(".big_box img").width()-$(".big_box").width())/(small_box_width - float_width);
115 
116             var percenty = ($(".big_box img").heitht()-$(".big_box").width())/(small_box_height - float_height);
117         })
118 
119 
120            $(".big_box img").css("left",-percentX*mouse_left + 'px');
121            $(".big_box img").css("top",-percenty*mouse_top + 'px');
122     })
123 
124 
125 
126 </script>
127 </body>
128 </html>
放大镜

 

 

3.6动画效果

  隐藏与显示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

</head>
<body>
    <!--1 隐藏与显示-->
    <!--2 淡入淡出-->
    <!--3 滑动-->
    <!--4 效果-回调:每一个动画执行完毕之后所能执行的函数方法或者所能做的一件事-->

    <p>hello</p>
    <button id="hide">隐藏</button>
    <button id="show">显示</button>
    <button id="toggle">隐藏/显示</button>



 <script src="jquery-3.1.0.js"></script>
    <script>

$(function(){
    $("#hide").click(function(){
        $("p").hide(1000);
    });
    $("#show").click(function(){
        $("p").show(1000);
    });

//用于切换被选元素的 hide() 与 show() 方法。
    $("#toggle").click(function(){
        $("p").toggle();
    })
    
});
    </script>
</body>
</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     
36     
37     </script>
38 
39 </head>
40 <body>
41       <button id="in">fadein</button>
42       <button id="out">fadeout</button>
43       <button id="toggle">fadetoggle</button>
44       <button id="fadeto">fadeto</button>
45 
46       <div id="id1" style="display:none; width: 80px;height: 80px;background-color: blueviolet"></div>
47       <div id="id2" style="display:none; width: 80px;height: 80px;background-color: orangered "></div>
48       <div id="id3" style="display:none; width: 80px;height: 80px;background-color: darkgreen "></div>
49 
50 </body>
51 </html>
淡入淡出

 

滑动

滑动

 

回调函数

在执行显示、隐藏、淡入、淡出等等操作的时候,可以调用一个回调函数

hide(1000,function(){xxxx})
 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>
回调函数

 

京东轮播图

  1 <!DOCTYPE html>
  2 <html lang="en">
  3 <head>
  4     <meta charset="UTF-8">
  5     <title>Title</title>
  6 
  7     <style>
  8         *{
  9     margin: 0;
 10     padding: 0;
 11 }
 12 ul{
 13     list-style: none;
 14 }
 15 
 16 .out{
 17     width: 730px;
 18     height: 454px;
 19     border: 8px solid mediumvioletred;
 20     margin: 50px auto;
 21     position: relative;
 22 }
 23 
 24 .out .img li{
 25     position: absolute;
 26     left: 0;
 27     top: 0;
 28 }
 29 
 30 .out .num{
 31     position: absolute;
 32     left:0;
 33     bottom: 20px;
 34     text-align: center;
 35     font-size: 0;
 36     width: 100%;
 37 }
 38 
 39 
 40 .out .btn{
 41     position: absolute;
 42     top:50%;
 43     margin-top: -30px;
 44     width: 30px;
 45     height: 60px;
 46     background-color: aliceblue;
 47     color: black;
 48     text-align: center;
 49     line-height: 60px;
 50     font-size: 40px;
 51     display: none;
 52 
 53 }
 54 
 55 .out .num li{
 56     width: 20px;
 57     height: 20px;
 58     background-color: grey;
 59     color: #fff;
 60     text-align: center;
 61     line-height: 20px;
 62     border-radius: 50%;
 63     display: inline;
 64     font-size: 18px;
 65     margin: 0 10px;
 66     cursor: pointer;
 67 }
 68 
 69 
 70 .out .left{
 71     left: 0;
 72 }
 73 .out .right{
 74     right: 0;
 75 }
 76 
 77 .out:hover .btn{
 78     display: block;
 79 }
 80 
 81 .out .num li.current{
 82     background-color: red;
 83 }
 84 
 85 
 86     </style>
 87 </head>
 88 <body>
 89      <div class="out">
 90          <ul class="img">
 91              <li><a href="#"><img src="image/1.jpg" alt=""></a></li>
 92              <li><a href="#"><img src="image/2.jpg" alt=""></a></li>
 93              <li><a href="#"><img src="image/3.jpg" alt=""></a></li>
 94              <li><a href="#"><img src="image/4.jpg" alt=""></a></li>
 95 
 96          </ul>
 97 
 98          <ul class="num">
 99              <li class="hide">1</li>
100              <li>2</li>
101              <li>3</li>
102              <li>4</li>
103 
104          </ul>
105 
106          <div class="left btn left_btn"><</div>
107          <div class="right btn right_btn">></div>
108 
109      </div>
110 
111 
112  <script src="jquery-3.1.0.js"></script>
113      <script>
114          $(function(){
115                  //手动点击下面的序号
116                  $('.num li').first().addClass("current");
117                  $(".num li").mouseover(function(){
118                  $(this).addClass("current");
119                      var index = $(this).index();
120                      i= index; //将手动和自定连接起来
121                      $(".img li").eq(index).fadeIn(1000);
122                      $(this).siblings().removeClass("current");
123                      $(".img li").eq(index).siblings().fadeOut(1000);
124 
125                  })
126 
127 
128              i= 0
129              //设置定时器,自动轮播
130              var time=setInterval(move,2000);
131              function move(){
132                  i++;
133                  if(i==4){ i =0;}
134                  $(".num li").eq(i).addClass("current");
135                  $(".num li").eq(i).siblings().removeClass("current");
136                  $(".img li").eq(i).fadeIn(1000);
137                  $(".img li").eq(i).siblings().fadeOut(1000);
138 
139              };
140 
141              // hover 有2个函数,进入和离开的时候
142              $(".out").hover(function(){
143                  clearInterval(time)
144 
145              },function(){
146                  time = setInterval(move,2000);
147              })
148 
149 
150 
151              //绑定一个事件,点击右侧按钮
152 
153              $(".right_btn").click(function(){
154                  move()
155              })
156 
157 
158              $(".left_btn").click(function(){
159                  if(i==0){i=4}
160                  i=i-2;
161                  move()
162              })
163 
164 
165          })
166 
167 
168 
169 
170      </script>
171     <!--<script>-->
172 
173 
174 <!--//手动轮播效果-->
175 <!--$(function(){-->
176     <!--var size=$(".img li").size()-->
177     <!--for (var i= 1;i<=size;i++){-->
178         <!--var li="<li>"+i+"</li>"-->
179         <!--$(".num").append(li);-->
180     <!--}-->
181 
182 
183     <!--//$(".img li").eq(0).show();-->
184     <!--$(".num li").eq(0).addClass("active");-->
185     <!--$(".num li").mouseover(function(){-->
186        <!--$(this).addClass("active").siblings().removeClass("active");-->
187        <!--var index=$(this).index();-->
188        <!--i=index;-->
189        <!--$(".img li").eq(index).fadeIn(1000).siblings().fadeOut(1000);-->
190    <!--});-->
191 
192 
193 <!--i=0;-->
194 <!--var t=setInterval(move,1500)-->
195 
196     <!--function move(){-->
197     <!--i++;-->
198     <!--if(i==size){-->
199         <!--i=0;-->
200     <!--}-->
201     <!--$(".num li").eq(i).addClass("active").siblings().removeClass("active");-->
202     <!--$(".img li").eq(i).stop().fadeIn(1000).siblings().stop().fadeOut(1000);-->
203 <!--};-->
204 
205     <!--function moveL(){-->
206     <!--i&#45;&#45;;-->
207     <!--if(i==-1){-->
208         <!--i=size-1;-->
209     <!--}-->
210     <!--$(".num li").eq(i).addClass("active").siblings().removeClass("active");-->
211     <!--$(".img li").eq(i).stop().fadeIn(1000).siblings().stop().fadeOut(1000);-->
212 <!--}-->
213 
214     <!--$(".out").hover(function(){-->
215         <!--clearInterval(t);-->
216     <!--},function(){-->
217         <!--t=setInterval(move,1500);-->
218     <!--});-->
219 
220     <!--$(".out .right").click(function(){-->
221         <!--move()-->
222     <!--});-->
223     <!--$(".out .left").click(function(){-->
224        <!--moveL()-->
225     <!--})-->
226 
227 <!--});-->
228     <!--</script>*/-->
229 </body>
230 </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 
 19 .header-nav {
 20     height: 39px;
 21     background: #c9033b;
 22 }
 23 .header-nav .bg{
 24     background: #c9033b;
 25 }
 26 
 27 .header-nav .nav-allgoods .menuEvent {
 28     display: block;
 29     height: 39px;
 30     line-height: 39px;
 31     text-decoration: none;
 32     color: #fff;
 33     text-align: center;
 34     font-weight: bold;
 35     font-family: 微软雅黑;
 36     color: #fff;
 37     width: 100px;
 38 }
 39 .header-nav .nav-allgoods .menuEvent .catName {
 40     height: 39px;
 41     line-height: 39px;
 42     font-size: 15px;
 43 }
 44 
 45 .header-nav .nav-allmenu a {
 46     display: inline-block;
 47     height: 39px;
 48     vertical-align: top;
 49     padding: 0 15px;
 50     text-decoration: none;
 51     color: #fff;
 52     float: left;
 53 }
 54 
 55 .header-menu a{
 56     color:#656565;
 57 }
 58 
 59 .header-menu .menu-catagory{
 60     position: absolute;
 61     background-color: #fff;
 62     border-left:1px solid #fff;
 63     height: 316px;
 64     width: 230px;
 65      z-index: 4;
 66      float: left;
 67 }
 68 .header-menu .menu-catagory .catagory {
 69     border-left:4px solid #fff;
 70     height: 104px;
 71     border-bottom: solid 1px #eaeaea;
 72 }
 73 .header-menu .menu-catagory .catagory:hover {
 74     height: 102px;
 75     border-left:4px solid #c9033b;
 76     border-bottom: solid 1px #bcbcbc;
 77     border-top: solid 1px #bcbcbc;
 78 }
 79 
 80 .header-menu .menu-content .item{
 81     margin-left:230px;
 82     position:absolute;
 83     background-color:white;
 84     height:314px;
 85     width:500px;
 86     z-index:4;
 87     float:left;
 88     border: solid 1px #bcbcbc;
 89     border-left:0;
 90     box-shadow: 1px 1px 5px #999;
 91 }
 92 
 93     </style>
 94 </head>
 95 <body>
 96 
 97 <div class="pg-header">
 98 
 99     <div class="header-nav">
100         <div class="container narrow bg">
101             <div class="nav-allgoods left">
102                 <a id="all_menu_catagory" href="#" class="menuEvent">
103                     <strong class="catName">全部商品分类</strong>
104                     <span class="arrow" style="display: inline-block;vertical-align: top;"></span>
105                 </a>
106             </div>
107         </div>
108     </div>
109     <div class="header-menu">
110         <div class="container narrow hide">
111             <div id="nav_all_menu" class="menu-catagory">
112                 <div class="catagory" float-content="one">
113                     <div class="title">家电</div>
114                     <div class="body">
115                         <a href="#">空调</a>
116                     </div>
117                 </div>
118                 <div class="catagory" float-content="two">
119                     <div class="title">床上用品</div>
120                     <div class="body">
121                         <a href="http://www.baidu.com">床单</a>
122                     </div>
123                 </div>
124                 <div class="catagory" float-content="three">
125                     <div class="title">水果</div>
126                     <div class="body">
127                         <a href="#">橘子</a>
128                     </div>
129                 </div>
130             </div>
131 
132             <div id="nav_all_content" class="menu-content">
133                 <div class="item hide" float-id="one">
134 
135                     <dl>
136                         <dt><a href="#" class="red">厨房用品</a></dt>
137                         <dd>
138                             <span>| <a href="#" target="_blank" title="勺子">勺子</a> </span>
139                         </dd>
140                     </dl>
141                     <dl>
142                         <dt><a href="#" class="red">厨房用品</a></dt>
143                         <dd>
144                             <span>| <a href="#" target="_blank" title="菜刀">菜刀</a> </span>
145 
146                         </dd>
147                     </dl>
148                     <dl>
149                         <dt><a href="#" class="red">厨房用品</a></dt>
150                         <dd>
151                             <span>| <a href="#">菜板</a> </span>
152                         </dd>
153                     </dl>
154                     <dl>
155                         <dt><a href="#" class="red">厨房用品</a></dt>
156                         <dd>
157                             <span>| <a href="#" target="_blank" title="碗">碗</a> </span>
158 
159                         </dd>
160                     </dl>
161 
162                 </div>
163                 <div class="item hide" float-id="two">
164                     <dl>
165                         <dt><a href="#" class="red">厨房用品</a></dt>
166                         <dd>
167                             <span>| <a href="#" target="_blank" title="">角阀</a> </span>
168 
169                         </dd>
170                     </dl>
171                     <dl>
172                         <dt><a href="#" class="red">厨房用品</a></dt>
173                         <dd>
174                             <span>| <a href="#" target="_blank" title="角阀">角阀</a> </span>
175 
176                         </dd>
177                     </dl>
178                     <dl>
179                         <dt><a href="#" class="red">厨房用品</a></dt>
180                         <dd>
181                             <span>| <a href="#" target="_blank" title="角阀">角阀</a> </span>
182 
183                         </dd>
184                     </dl>
185 
186                 </div>
187                 <div class="item hide" float-id="three">
188                     <dl>
189                         <dt><a href="#" class="red">厨房用品3</a></dt>
190                         <dd>
191                             <span>| <a href="#" target="_blank" title="角阀">角阀3</a> </span>
192 
193                         </dd>
194                     </dl>
195                     <dl>
196                         <dt><a href="#" class="red">厨房用品3</a></dt>
197                         <dd>
198                             <span>| <a href="http://www.meilele.com/category-jiaofa/" target="_blank" title="角阀">角阀3</a> </span>
199 
200                         </dd>
201                     </dl>
202                 </div>
203             </div>
204         </div>
205     </div>
206 
207 </div>
208 
209 
210 <script src="js/jquery-2.2.3.js"></script>
211 
212 <script type="text/javascript">
213     $(document).ready(function () {
214 
215         Change_Menu('#all_menu_catagory','#nav_all_menu', '#nav_all_content');
216 
217     });
218 
219 
220 
221     function Change_Menu(all_menu_catagory,menu, content) {
222         $all_menu_catagory = $(all_menu_catagory);
223         $menu = $(menu);
224         $content = $(content);
225 
226         $all_menu_catagory.bind("mouseover", function () {
227             $menu.parent().removeClass('hide');
228         });
229         $all_menu_catagory.bind("mouseout", function () {
230             $menu.parent().addClass('hide');
231         });
232 
233         $menu.children().bind("mouseover", function () {
234             $menu.parent().removeClass('hide');
235             $item_content = $content.find('div[float-id="' + $(this).attr("float-content") + '"]');
236             $item_content.removeClass('hide').siblings().addClass('hide');
237         });
238         $menu.bind("mouseout", function () {
239             $content.children().addClass('hide');
240             $menu.parent().addClass('hide');
241         });
242         $content.children().bind("mouseover", function () {
243 
244             $menu.parent().removeClass('hide');
245             $(this).removeClass('hide');
246         });
247         $content.children().bind("mouseout", function () {
248 
249             $(this).addClass('hide');
250             $menu.parent().addClass('hide');
251         });
252     }
253 </script>
254 
255 
256 </body>
257 </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="jquery-3.1.0.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             //如果原来是选中状态,退出编辑模式
102             //如果原来是非选中,进入编辑模式
103             //由于checkbox本身的点击事件优先于绑定事件
104             //所以在选中的时刻上,checkbox会先执行check,才会触发事件
105             //p标签上绑定事件优先于点击事件
106             if($(this).prop('checked')){
107                 RowIntoEdit($tr);
108             }else{
109                 RowOutEdit($tr);
110             }
111         }
112     });
113 }
114 
115 function CreateSelect(attrs,csses,option_dict,item_key,item_value,current_val){
116     var sel= document.createElement('select');
117     $.each(attrs,function(k,v){
118         $(sel).attr(k,v);
119     });
120     $.each(csses,function(k,v){
121         $(sel).css(k,v);
122     });
123     $.each(option_dict,function(k,v){
124         var opt1=document.createElement('option');
125         var sel_text = v[item_value];
126         var sel_value = v[item_key];
127 
128         if(sel_value==current_val){
129             $(opt1).text(sel_text).attr('value', sel_value).attr('text', sel_text).attr('selected',true).appendTo($(sel));
130         }else{
131             $(opt1).text(sel_text).attr('value', sel_value).attr('text', sel_text).appendTo($(sel));
132         }
133     });
134     return sel;
135 }
136 
137 STATUS = [
138     {'id': 1, 'value': "在线"},
139     {'id': 2, 'value': "下线"}
140 ];
141 
142 BUSINESS = [
143     {'id': 1, 'value': "车上会"},
144     {'id': 2, 'value': "二手车"}
145 ];
146 
147 function RowIntoEdit($tr){
148     $tr.children().each(function(){
149         if($(this).attr('edit') == "true"){
150             if($(this).attr('edit-type') == "select"){
151                 var select_val = $(this).attr('sel-val');
152                 var global_key = $(this).attr('global-key');
153                 var selelct_tag = CreateSelect({"onchange": "MultiSelect(this);"}, {}, window[global_key], 'id', 'value', select_val);
154                 $(this).html(selelct_tag);
155             }else{
156                 var orgin_value = $(this).text();
157                 var temp = "<input value='"+ orgin_value+"' />";
158                 $(this).html(temp);
159             }
160 
161         }
162     });
163 }
164 
165 function RowOutEdit($tr){
166     $tr.children().each(function(){
167         if($(this).attr('edit') == "true"){
168             if($(this).attr('edit-type') == "select"){
169                 var new_val = $(this).children(':first').val();
170                 var new_text = $(this).children(':first').find("option[value='"+new_val+"']").text();
171                 $(this).attr('sel-val', new_val);
172                 $(this).text(new_text);
173             }else{
174                 var orgin_value = $(this).children().first().val();
175                 $(this).text(orgin_value);
176             }
177 
178         }
179     });
180 }
181 
182 function CheckAll(mode, tb){
183     //用于监测用户是否点击了编辑模式
184     //tb表示table中的tbody
185     if($(mode).hasClass('editing')){
186 
187         $(tb).children().each(function(){
188 
189             var tr = $(this);
190             var check_box = tr.children().first().find(':checkbox');
191             if(check_box.prop('checked')){
192 
193             }else{
194                 check_box.prop('checked',true);
195 
196                 RowIntoEdit(tr);
197             }
198         });
199 
200     }else{
201 
202         $(tb).find(':checkbox').prop('checked', true);
203     }
204 }
205 
206 function CheckReverse(mode, tb){
207 
208     if($(mode).hasClass('editing')){
209 
210         $(tb).children().each(function(){
211             var tr = $(this);
212             var check_box = tr.children().first().find(':checkbox');
213             if(check_box.prop('checked')){
214                 check_box.prop('checked',false);
215                 RowOutEdit(tr);
216             }else{
217                 check_box.prop('checked',true);
218                 RowIntoEdit(tr);
219             }
220         });
221 
222 
223     }else{
224         //
225         $(tb).children().each(function(){
226             var tr = $(this);
227             var check_box = tr.children().first().find(':checkbox');
228             if(check_box.prop('checked')){
229                 check_box.prop('checked',false);
230             }else{
231                 check_box.prop('checked',true);
232             }
233         });
234     }
235 }
236 
237 function CheckCancel(mode, tb){
238     if($(mode).hasClass('editing')){
239         $(tb).children().each(function(){
240             var tr = $(this);
241             var check_box = tr.children().first().find(':checkbox');
242             if(check_box.prop('checked')){
243                 check_box.prop('checked',false);
244                 RowOutEdit(tr);
245 
246             }else{
247 
248             }
249         });
250 
251     }else{
252         $(tb).find(':checkbox').prop('checked', false);
253     }
254 }
255 
256 function EditMode(ths, tb){
257     if($(ths).hasClass('editing')){
258         $(ths).removeClass('editing');
259         $(tb).children().each(function(){
260             var tr = $(this);
261             var check_box = tr.children().first().find(':checkbox');
262             if(check_box.prop('checked')){
263                 RowOutEdit(tr);
264             }else{
265 
266             }
267         });
268 
269     }else{
270 
271         $(ths).addClass('editing');
272         $(tb).children().each(function(){
273             var tr = $(this);
274             var check_box = tr.children().first().find(':checkbox');
275             if(check_box.prop('checked')){
276                 RowIntoEdit(tr);
277             }else{
278 
279             }
280         });
281 
282     }
283 }
284 
285 
286     </script>
287 
288 </body>
289 </html>
编辑框

 

posted @ 2016-08-13 16:30  咖啡茶  阅读(146)  评论(0)    收藏  举报