Home >Web Front-end >JS Tutorial >jquery realizes the function of moving the list up and down_jquery

jquery realizes the function of moving the list up and down_jquery

PHP中文网
PHP中文网Original
2016-05-16 15:13:361606browse

Without further ado, let’s get to the topic.
What we implemented today is a function to move up and down the list page. As shown in the picture:

jquery realizes the function of moving the list up and down_jquery

When a column in the list is checked, click Move Up or Move Down, and it will move up or down dynamically.
The html code is as follows:

<div> 
  <input type="button" onclick="up();" value=" 上移 "> 
  <input type="button" onclick="down();" value=" 下移 "> 
</div> 
 <div> 
<table width="400px" height="200" class="mytable" cellpadding="5" cellspacing="0"> 
<tr> 
 <td>序号</td> 
 <td>名字</td> 
  <td>性别</td> 
</tr> 
 <tr> 
 <td><input type="checkbox" id="c1"/>1</td> 
 <td>小一</td> 
  <td>男</td> 
</tr> 
 <tr> 
 <td><input type="checkbox" id="c2"/>2</td> 
 <td>小二</td> 
  <td>女</td> 
</tr> 
<tr> 
 <td><input type="checkbox" id="c3"/>3</td> 
 <td>小三</td> 
  <td>女</td> 
</tr> 
</table> 
lt;/div>


We define a css style called mytable

 font-size:12px; 
 color:red; 
 border:1px solid #000; 
 text-align:center; 
 border-collapse:collapse; 
 }

Then implement the up() and down() methods. The code is as follows:

$.each($("table input:checked"),function(){ 
  var onthis=$(this).parent().parent(); 
  var getUp=onthis.prev(); 
   
  if ($(getUp).has("input").size()==0) 
  { 
   alert("顶级元素不能上移"); 
   return; 
  } 
  $(onthis).after(getUp); 
 }); 
} 
 function down(){ 
 $.each($("table input:checked"),function(){ 
  var onthis=$(this).parent().parent(); 
  var getdown=onthis.next(); 
  $(getdown).after(onthis); 
 }); 
}

Use the jquery provided The function is very simple to implement. Of course, if you want to move multiple columns up and down at the same time, you only need to add a loop. The core code is as above.

The above is the content of jquery to implement the function of moving the list up and down_jquery. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn