基于jq的全选反选
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<!-- 引入js库 -->
<script src="jquery/jquery-1.11.3.js"></script>
</head>
<body>
<input type="checkbox" name="ck">葵花宝典<br>
<input type="checkbox" name="ck">辟邪剑谱<br>
<input type="checkbox" name="ck">九阴真经<br>
<input type="checkbox" name="ck">乾坤大挪移<br>
<input type="checkbox" name="ck">九阳神功<br>
<input type="checkbox" name="" id="selectAll">全选<br>
<input type="checkbox" name="" id="selectNone">不选<br>
<input type="checkbox" name="" id="selectReverse">反选<br>
<script>
$(function(){
$('#selectAll').click(function(){
$('input[name=ck]').prop('checked',$(this).prop('checked'));
})
$('#selectNone').click(function(){
if($(this).prop('checked')){
$('input[name=ck]').attr('checked',false);
}
})
$('#selectReverse').click(function(){
if($(this).prop('checked')){
$('input[name=ck]').each(function(i,e){
$(e).prop('checked',!$(e).prop('checked'));
})
}
})
})
</script>
</body>
</html>
可编辑表格实现
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<!-- 引入js库 -->
<script src="jquery/jquery-1.11.3.js"></script>
</head>
<body>
<input type="checkbox" name="ck">葵花宝典<br>
<input type="checkbox" name="ck">辟邪剑谱<br>
<input type="checkbox" name="ck">九阴真经<br>
<input type="checkbox" name="ck">乾坤大挪移<br>
<input type="checkbox" name="ck">九阳神功<br>
<input type="checkbox" name="" id="selectAll">全选<br>
<input type="checkbox" name="" id="selectNone">不选<br>
<input type="checkbox" name="" id="selectReverse">反选<br>
<script>
$(function(){
$('#selectAll').click(function(){
$('input[name=ck]').prop('checked',$(this).prop('checked'));
})
$('#selectNone').click(function(){
if($(this).prop('checked')){
$('input[name=ck]').attr('checked',false);
}
})
$('#selectReverse').click(function(){
if($(this).prop('checked')){
$('input[name=ck]').each(function(i,e){
$(e).prop('checked',!$(e).prop('checked'));
})
}
})
})
</script>
</body>
</html>
事件绑定与事件委托
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="jquery/jquery-1.11.3.js"></script>
</head>
<body>
<div id="app">
<button id="btn1">默认按钮</button>
</div>
<script>
$(function(){
$('#btn1').click(function(){
$('#app').append('<button class="btn">生成按钮</button>');
return false;
})
$('#app').on('click','.btn',function(){
console.info('btn click');
})
})
</script>
</body>
</html>
动态菜单实现
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
*{
margin:0;
padding:0;
}
ul{
list-style: none;
}
#menu>li{
float:left;
margin:0 1px 0 0;
width:200px;
height: 50px;
line-height: 50px;
text-align: center;
background-color: #27ae60;
}
a{
display: block;
font-size: 1.1em;
color:#eee;
text-decoration: none;
}
.submenu{
background-color: #2ecc71;
display: none;
}
.submenu>li{
border-bottom: 1px solid rgba(255,255,255,.3);
}
.submenu>li:hover{
background-color:rgba(0,0,0,0.2);
}
</style>
<script src="jquery/jquery-1.11.3.js"></script>
</head>
<body>
<ul id="menu">
<li>
<a href="">menu1</a>
<ul class="submenu">
<li><a href="">submenu1-1</a></li>
<li><a href="">submenu1-2</a></li>
</ul>
</li>
<li>
<a href="">menu2</a>
<ul class="submenu">
<li><a href="">submenu2-1</a></li>
<li><a href="">submenu2-2</a></li>
</ul>
</li>
<li>
<a href="">menu3</a>
<ul class="submenu">
<li><a href="">submenu3-1</a></li>
<li><a href="">submenu3-2</a></li>
</ul>
</li>
</ul>
<script>
$(function(){
$('#menu>li').hover(function(){
$(this).find('.submenu:not(:animated)').slideDown();
},function(){
$(this).find('.submenu:not(:animated)').slideUp();
})
})
</script>
</body>
</html>