让你的input和td都可以选择

2011-03-28 13:02:39 by 【6yang】, 175 visits, 收藏 | 返回

<!DOCTYPE html>
<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<style>
  .slt{background-color:#000; color:#FFF;}
</style>
  <script>
   function doSomething (obj,evt) {
 //alert(obj.id);
 if($(obj).attr("checked")){
  $(obj).parent().parent().addClass("slt");
}
 else{
  $(obj).parent().parent().removeClass("slt");   
}
 var e=(evt)?evt:window.event;
 if (window.event) {
  e.cancelBubble=true;     // ie?????
 } else {
  //e.preventDefault();
  e.stopPropagation();     // ??????????
 }
}
    
  $(function(){
    $("#tb tr").toggle(
      function(){
        $(this).addClass("slt");
        $(this).find(":checkbox").attr("checked","checked");
      },
      function(){
        $(this).removeClass("slt");  
        $(this).find(":checkbox").attr("checked","");
      }
    )
  })
</script>
</head>
<body>
  <form id="form1" name="form1" method="post" action="">
  <table width="788" border="0" cellspacing="0" cellpadding="0" id="tb">
    <tr>
      <td width="73" height="30">&nbsp;</td>
      <td width="165">&nbsp;</td>
      <td width="550">&nbsp;</td>
    </tr>
    <tr>
      <td height="30" align="center"><input type="checkbox" name="checkbox" id="checkbox"onclick="doSomething(this,event);" /></td>
      <td>111111111</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td height="30" align="center"><input type="checkbox" name="checkbox2" id="checkbox2"onclick="doSomething(this,event);" /></td>
      <td>222222</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td height="30" align="center"><input type="checkbox" name="checkbox3" id="checkbox3"onclick="doSomething(this,event);" /></td>
      <td>33333</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td height="30" align="center"><input type="checkbox" name="checkbox4" id="checkbox4"onclick="doSomething(this,event);" /></td>
      <td>33444444</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td height="30" align="center"><input type="checkbox" name="checkbox5" id="checkbox5"onclick="doSomething(this,event);" /></td>
      <td>5555555555</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td height="30" align="center"><input type="checkbox" name="checkbox6" id="checkbox6"onclick="doSomething(this,event);" /></td>
      <td>66666666</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td height="30" align="center"><input type="checkbox" name="checkbox7" id="checkbox7"onclick="doSomething(this,event);" /></td>
      <td>6777777777</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td height="30" align="center"><input type="checkbox" name="checkbox8" id="checkbox8"onclick="doSomething(this,event);" /></td>
      <td>888888</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td height="30" align="center"><input type="checkbox" name="checkbox9" id="checkbox9"onclick="doSomething(this,event);" /></td>
      <td>9999999</td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form>
</body>
</html>

建议:input type=checkbox 做成图片;效率会高点.

测试效果:http://jsbin.com/ocuti4/13/edit

分享到:
share

    图片原图

    loading

    loading