jquery事件处理、查找、强大的表格变换颜色案例

<!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" />
<title>toggle第几第一次执行第一个函数,点击第二次执行第二个函数</title>
<style type="text/css">
.even{
	background-color:#CCC;}
.odd{
	background-color:#FFff99;}
 .selected{
	 background-color:#03F;}

 .checks{
	 background-color:#C39;}
	 
  .huadong{
	  background-color:#3C3;}	 
</style>
<script type="text/javascript" src="../include/jquery.js"></script>
<script type="text/javascript">
var color;
//使用jquery加载事件
 $(document).ready(function (){
	
	//toggleClass()相当于在匹配元素上加上了class属性
	//style比class优先 所以style>class  id>class
	
	//偶数	
	$("tr:even").addClass("even");
	
	//奇数
	$("tr:odd").addClass("odd");
	
	//设置滑动
	$("tr").mouseover(function (){
		
		$(this).addClass("huadong");
		});
		
   	$("tr").mouseout(function (){
		
		$(this).removeClass("huadong");
		});		
	
	//设置点击换色
	/*$("tr").click(function (){
		$(this).toggleClass("selected");
		});*/
	
	
	//用上边的可以代替,上边的简单,效果一样
	$("input").each(function (){
		
		$(this.parentNode.parentNode).click(function (){
			$(this).toggleClass("selected");
			});
		});
		
		
		
		
		
	/*$("tr").toggle(function(){
		
		$(this).toggleClass("selected");
		
		},function(){
			
			$(this).removeClass("selected");
			});*/
	
   
   //选中checkbox换色
	//$("tr").mouseover(function (){	
	//alert("aa");
	$("input[type=checkbox]").each(function (index,domEle){
		
	$("input[type=checkbox]").click(function (){
		if(this.checked==true){
		$(this).parents("tr").addClass("checks")}
		
		if(this.checked==false)	{
			 $(this).parents("tr").removeClass("checks");
			}		
		 });
		
	}); 
	
	
	
	
	//设置radio选中变色	
	$("input[type=radio]").click(function (){
		$("input[type=radio]").each(function (i){
			$(this).parents("tr").removeClass("selected");
		if(this.checked==true){
			
		   $(this).parents("tr").toggleClass("checks");
		  
		}else{
			
			  $(this).parents("tr").removeClass("checks");
			
			}
					
		 });
		
	  });
	
	
	
 });
 

 
</script>
</head>

<body>
<form  method="post">

 <table width="397" border="1">
  <tr>
    <td width="56"><input type="checkbox" id="checkbox"></td>
    <td width="160"> </td>
    <td width="159"> </td>
  </tr>
  
  <tr>
    <td><input type="checkbox" id="checkbox"></td>
    <td> </td>
    <td> </td>
  </tr>
  
  <tr>
    <td><input type="checkbox" id="checkbox"></td>
    <td> </td>
    <td> </td>
  </tr>
  
  <tr>
    <td><input type="checkbox" id="checkbox"></td>
    <td> </td>
    <td> </td>
  </tr>
  
  <tr>
    <td><input type="checkbox" id="checkbox"></td>
    <td> </td>
    <td> </td>
  </tr>
  
  <tr>
    <td><input type="radio" name="radio"></td>
    <td> </td>
    <td> </td>
  </tr>
  
  <tr>
    <td><input type="radio" name="radio"></td>
    <td> </td>
    <td> </td>
  </tr>
  
  <tr>
    <td><input type="radio" name="radio"></td>
    <td> </td>
    <td> </td>
  </tr>
</table>

  </form> 
   
</body>
</html>


事件处理

   bind:为每个匹配元素的特定事件绑定事件处理函数。

   

     1

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

           alert( $(this).text() );

          });

 

     2:调用dom函数

 

       $("input[type=button]").bind("dblclick",aa);

 

      function aa(){

        

        alert("aa");

        }

 

   one:绑定一个一次性的事件处理函数。

   

     :给段落表记绑定一个一次性的事件,只能调用一次

 

       $("p").one("click",function(){

                    

                     alert($(this).text());

                     });

 

 

查找

 

   children():每一个元素的所有子元素的元素集合。

 

     :查找DIV中的每个子元素。

 

    $("#d2").children().each(function (index,domEle){

                    

                     alert(domEle.value);

                     });

 

  find():

 

    搜索所有与指定表达式匹配的元素。

   

    $("p").find("span").css("background-color","green");

 

  next():案例折叠

 

      取得一个包含匹配的元素集合中每一个元素紧邻的后面同辈 

    元素的元素集合。

 

     $("span").click(function (){

 

        $(this).next().toggle("slow");

             

               }); 

 

 

  parent():

     取得一个包含着所有匹配元素的唯一父元素的元素集合。

 

    $("a").mouseover(function (){

                    

      $(this).parent().css("background","green");

                     });

 

  parents():重要

  

    用于筛选祖先元素的表达式

 

    $("span").parents(".d2").css("background","green");

 

val():

 

   1:选中单选框radioval值等于3

 

 

    $("input[name=rad]").each(function (index,domEle){

                    

       if(domEle.value=='3'){domEle.checked=true;}

                    

                     });

 

   2:默认选中下拉菜单select2

  

       $("#sel2").val("2");

 

   3:默认下拉菜单同时选中2号和5"

    

       $("select").eq(1).val(["2","5"]);    

 

   4:默认复选框选中2号和5

 

       $("input[name=box]").val(["2","5"]);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值