jquery--各种小技巧集合

1.获得所有选中的checkbox的方法

//根据传入的checkbox的name获得所有checkbox选取的的值
function addMem(names){
var allNames=0;
$(
"input[name='"+names+"']").each(function(){
if($(this).attr("checked")=="checked"){
if(allNames==0){
allNames
=$(this).attr("value");
}
else{
allNames
+= ","+$(this).attr("value");
}
}
});
alert(allNames);
return allNames;
}

2.复选框全部选定或者全部不选定

function selectAll(){
$(
"#deleteAll").change(function(){
if($(this).attr("checked")=="checked"){
$(
"input[name='deleteCheck']").each(function(){
$(
this).attr("checked",true);
});
}
else{
$(
"input[name='deleteCheck']").each(function(){
$(
this).attr("checked",false);
});
}
});
}

3.另一种全选,全不选,反选的方式(与原生js混合使用)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript" src="../css02/js/jquery-1.6.1.js"></script>
<script>
$(document).ready(
function(){

$(
"#selectAll").click(function(){
//这里也可以直接用this,原生js语句
if($(this)[0].checked){

$(
'[name=items]:checkbox').attr("checked",true);
}
else{
$(
'[name=items]:checkbox').attr("checked",false);
}
});
$(
"#XOR").click(function(){
$(
"[name=items]:checkbox").each(function(){
this.checked=!this.checked;
});
});
});
</script>
<title>Untitled Document</title>
</head>

<body>
<input type="checkbox" name="items" value="足球" />足球
<input type="checkbox" name="items" value="足球" />足球
<input type="checkbox" name="items" value="足球" />足球
<input type="checkbox" name="items" value="足球" />足球
<input type="checkbox" id="selectAll" value="全选/不选"/>全选/全部选
<input type="button" id="XOR" value="反选"/>
</body>
</html>

4.表格分层折叠

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="../css/jquery-1.6.2.js"></script>
<script>
$(
function(){
$(
'tr.parent').click(function(){
$(
this).toggleClass("selected").siblings('.child_'+this.id).toggle();

});
});
</script>
<style>
.selected
{background:red};
</style>
<title>Untitled Document</title>
</head>

<body>
<table>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<tr class="parent" id="row_01">
<td colspan="2">人力部</td>
</tr>
<tr class="child_row_01">
<td >张三</td>
<td>12</td>
</tr>
<tr class="child_row_01">
<td >张三</td>
<td >12</td>
</tr>
<tr class="child_row_01">
<td >张三</td>
<td >12</td>
</tr>
<tr class="parent" id="row_02">
<td colspan="2">技术部</td>
</tr>
<tr class="child_row_02">
<td >张三</td>
<td >12</td>
</tr>
<tr class="child_row_02">
<td >张三</td>
<td >12</td>
</tr>
<tr class="child_row_02">
<td >张三</td>
<td >12</td>
</tr>
</tbody>
</table>
</body>
</html>

表格内容筛选

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="../css/jquery-1.6.2.js"></script>
<script>
$(
function(){
$(
"#confirm").click(function(){
var txt=$("[name=fileterText]").val();
//思路是先全部隐藏,然后根据内容再显示出来
$("table tbody tr").hide().filter(":contains("+txt+")").show();
});
});
</script>
<style>
.selected
{background:red};
</style>
<title>Untitled Document</title>
</head>

<body>
<input type="text" name="fileterText"/><input type="button" value="Commit" id="confirm"/>
<table>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
</tr>
</thead>
<tbody>
<tr class="parent" id="row_01">
<td colspan="2">人力部</td>
</tr>
<tr class="child_row_01">
<td >张三</td>
<td>12</td>
</tr>
<tr class="child_row_01">
<td >李四</td>
<td >12</td>
</tr>
<tr class="child_row_01">
<td >张三</td>
<td >12</td>
</tr>
<tr class="parent" id="row_02">
<td colspan="2">技术部</td>
</tr>
<tr class="child_row_02">
<td >张三</td>
<td >12</td>
</tr>
<tr class="child_row_02">
<td >张三</td>
<td >12</td>
</tr>
<tr class="child_row_02">
<td >张三</td>
<td >12</td>
</tr>
</tbody>
</table>
</body>
</html>

更改点击的li的样式。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="../css/jquery-1.6.2.js"></script>
<script>
$(
function(){
$(
"ul li").each(function(){
$(
this).click(function(){
$(
this).addClass("selected").siblings().removeClass("selected");
});
});
});
</script>
<style>
.selected
{background:red};
</style>
<title>Untitled Document</title>
</head>

<body>
<ul>
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
</ul>
</body>
</html>

posted on 2011-08-22 08:00    阅读(300)  评论(0编辑  收藏  举报

导航