html鼠标选择一个区域,把该区域checkbox 都选中

Q: html鼠标选择一个区域,把该区域checkbox 都选中

A:

效果一般

ContractedBlock.gif ExpandedBlockStart.gif Code
<!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>
    
<title></title>
    
<script language="javascript">
        
var downX;
        
var downY;
        
var isSelecting = false;
        
function mUp(ev) {

            
var region = document.getElementById("region");
            region.style.display 
= "none";
            
            
if (!ev) ev = window.event;
            
if (isSelecting) {
                isSelecting 
= false;
                
var inputs = document.getElementsByTagName("input");
                
for (i = 0; i < inputs.length; i++) {
                    
if (inputs[i].type == "checkbox") {
                        
if (jiechu(downX, downY, ev.clientX, ev.clientY, inputs[i])){
                            inputs[i].checked 
= true;
                        }
                    }
                }
            }
        }
        
function jiechu(ax, ay, bx, by, cb) { 
            
var cbx=getX(cb);
            
var cby = getY(cb);
            
var cbw = cb.clientWidth;
            
var cbh = cb.clientHeight;
            
if ((cbx - ax) * (cbx - bx) < 0 && (cby - ay) * (cby - by) < 0return true;
            
if ((cbx - ax) * (cbx - bx) < 0 && ((cby + cbh) - ay) * ((cby + cbh) - by) < 0return true;
            
if (((cbx + cbw) - ax) * ((cbx + cbw) - bx) < 0 && (cby - ay) * (cby - by) < 0return true;
            
if (((cbx + cbw) - ax) * ((cbx + cbw) - bx) < 0 && ((cby + cbh) - ay) * ((cby + cbh) - by) < 0return true;
            
return false;
            
        }
        
function getX(obj) {
            
return obj.offsetLeft + (obj.offsetParent ? getX(obj.offsetParent) : obj.x ? obj.x : 0);
        }
        
function getY(obj) {
            
return (obj.offsetParent ? obj.offsetTop + getY(obj.offsetParent) : obj.y ? obj.y : 0);
        }  
        
function mDown(ev) {
            
if (!ev) ev = window.event;
            isSelecting 
= true;
            downX 
= ev.clientX;
            downY 
= ev.clientY;

            
var region = document.getElementById("region");
            region.style.top 
= downY + "px";
            region.style.left 
= downX + "px";
            region.style.display 
= "block";
        }
        
function mMove(ev) {

            
if (!ev) ev = window.event;

            
var region = document.getElementById("region");
            
if (ev.clientX > downX) {
                region.style.left 
= downX + "px";
            }
            
else {
                region.style.left 
= ev.clientX + "px";
            }
            
if (ev.clientY > downY) {
                region.style.top 
= downY + "px";
            }
            
else {
                region.style.top 
= ev.clientY + "px";
            }
            region.style.width 
= Math.abs(ev.clientX - downX) + "px";
            region.style.height 
= Math.abs(ev.clientY- downY) + "px";
        }
    
</script>
</head>
<body onmousedown="mDown(event)" onmousemove="mMove(event)" onmouseup="mUp(event)">
    
<div id="region" style="position:absolute;top:0px;left:0px;border:1px red solid;display:none"></div>
    
<table id="tbl">
        
<tr>
            
<td>
                
<input id="Checkbox1" type="checkbox" />aa
            
</td>
            
<td>
                
<input id="Checkbox2" type="checkbox" />bb
            
</td>
            
<td>
                
<input id="Checkbox3" type="checkbox" />cc
            
</td>
        
</tr>
        
<tr>
            
<td>
                
<input id="Checkbox4" type="checkbox" />dd
            
</td>
            
<td>
                
<input id="Checkbox5" type="checkbox" />ee
            
</td>
            
<td>
                
<input id="Checkbox6" type="checkbox" />ff
            
</td>
        
</tr>
        
<tr>
            
<td>
                
<input id="Checkbox7" type="checkbox" />gg
            
</td>
            
<td>
                
<input id="Checkbox8" type="checkbox" />hh
            
</td>
            
<td>
                
<input id="Checkbox9" type="checkbox" />ii
            
</td>
        
</tr>
    
</table>
</body>
</html>

转载于:https://www.cnblogs.com/kdyang/archive/2009/05/13/1455656.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现鼠标点击checkbox后改变tr背景颜色的效果,你可以使用JavaScript来完成。 首先,给checkbox添加一个点击事件监听,当checkbox被点击时触发相应的函数。在该函数中,你可以使用条件语句来切换tr元素的背景颜色。 以下是一个示例的HTML代码: ```html <!DOCTYPE html> <html> <head> <title>Change TR background color on checkbox click</title> <style> .selected { background-color: yellow; } </style> </head> <body> <table> <tr onclick="changeBackgroundColor(this)"> <td><input type="checkbox"></td> <td>Row 1</td> </tr> <tr onclick="changeBackgroundColor(this)"> <td><input type="checkbox"></td> <td>Row 2</td> </tr> <tr onclick="changeBackgroundColor(this)"> <td><input type="checkbox"></td> <td>Row 3</td> </tr> </table> <script> function changeBackgroundColor(row) { var checkbox = row.querySelector('input[type="checkbox"]'); if (checkbox.checked) { row.classList.add('selected'); } else { row.classList.remove('selected'); } } </script> </body> </html> ``` 在上面的示例中,每个tr元素都有一个点击事件监听器'onclick',当被点击时会调用changeBackgroundColor函数。该函数首先获取到被点击行中的checkbox元素,然后根据checkbox的checked属性来切换tr元素的类名。如果checkbox选中,就会添加一个名为'selected'的类,该类定义了背景颜色为黄色。如果checkbox未被选中,就会移除'selected'类,恢复默认背景颜色。 你可以根据自己的需求修改示例代码中的样式和HTML结构。希望这能帮到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值