天天看点

用js实现shift+单击来选择多项

?变?宸ヤ???瑕?锛???eb椤圭??????涓??归??澶??????戒腑浣跨??s?ュ????hift+?瑰?诲乏?????╄?缁???澶?椤癸???杩??ラ??浜?涓?缃?涓?璧?婧?锛???浜?涓?绉?绠?????瀹??版?瑰?锛?杩?????涓?绠?????璁板????变韩??

???????? 涓昏???????璧?婧?锛?http://blog.sina.com.cn/s/blog_94eb1a960100zl7z.html

???????? 涓昏?娑????扮??????锛?html??? javascript??? jquery(杩???涓轰???js?逛究灏卞???ヤ?璇ユ???

???????? 璇ユ??浣???瀹??版???筹?

?????????????????? 1.璁板?涓?姣?娆$?瑰?讳?浠剁????绱??ㄥ??琛ㄤ腑??绱㈠?锛??惧?ユ?扮?涓?

?????????????????? 2.?ゆ???瑰?讳?浠舵??hift???????涓?锛?濡?琚???涓??????烘?扮?涓??????惧?ョ??涓や釜绱㈠?

?????????????????? 3.?规?????虹??绱㈠?锛??ュ???哄?ㄨ?ョ储寮????村????琛ㄤ腑????绱?锛?骞舵?规????瑕???澶???

???????? 涓??㈡??绠?????瀹??颁唬??锛?

<!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=gb2312" />
<title>????棰???妗?lt;/title>
<script type="text/javascript" src="jquery-1.10.2.js"></script>
<script type="text/javascript">
var rem=new Array();
    function shift_select(){
        $(".box div").click(function (e){
            rem.push($(".box div").index($(this)))
            if(e.shiftKey){
                var iMin =  Math.min(rem[rem.length-2],rem[rem.length-1])
                var iMax =  Math.max(rem[rem.length-2],rem[rem.length-1])
                for(i=iMin;i<=iMax;i++){
                    $(".box div:eq("+i+") input[type=checkbox]").prop("checked",true);
			        //杩?杈归?ㄥ??涓洪?昏?浠g??锛?浣???浠ユ?规????瑕??ュ????ㄤ腑??div
                   //$(".box div:eq("+i+")")杩?涓洪??涓???div
                }
            }else{
                $(this).toggleClass("selected");
            }
        });
    }
    $(function(){
        shift_select();
    });
   
//涓?绋?搴????筹???绂?姝㈤?????涓???瀛????归?????跺??瀹规????涓???瀛? 澶?璁ㄥ?? ??
document.onselectstart=function(event){
    event = window.event||event;
    event.returnValue = false;
}
</script>
<style type="text/css">
    .selected{background:tan;}
    p{ cursor:pointer; background:#f0f0f0}
</style>
</head>
<body>
<div class="box">
<div><input type="checkbox" value="1" />hello1</div>
<div><input type="checkbox" value="1" />hello2</div>
<div><input type="checkbox" value="1" />hello3</div>
<div><input type="checkbox" value="1" />hello4</div>
<div><input type="checkbox" value="1" />hello5</div>
<div><input type="checkbox" value="1" />hello6</div>
<div><input type="checkbox" value="1" />hello7</div>
<div><input type="checkbox" value="1" />hello8</div>
<div><input type="checkbox" value="1" />hello9</div>
<div><input type="checkbox" value="1" />hello10</div>
<div><input type="checkbox" value="1" />hello11</div>
<div><input type="checkbox" value="1" />hello12</div>
<div><input type="checkbox" value="1" />hello13</div>
<div><input type="checkbox" value="1" />hello14</div>
<div><input type="checkbox" value="1" />hello15</div>
<div><input type="checkbox" value="1" />hello16</div>
<div><input type="checkbox" value="1" />hello17</div>
<div><input type="checkbox" value="1" />hello18</div>
</div>
</body>
</html>
           

娉ㄦ??浜?椤癸?

???????? 1.浠ヤ?浠g??涓???瑕?寮???query??浠讹?缃?缁?jquery??浠跺?板??涓猴?

?????????????????? http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js

???????? ? ?ㄨ?ュ?板??浠f??lt;scripttype="text/javascript"src="jquery-1.10.2.js"></script>涓?src?肩???ㄥ??灏辫?锛?涓?杩?璁块??姣?杈????寤鸿??寮??ユ???扮??jquery??浠讹?涓??????渚?demo??涓?杞藉?板??锛????㈡??jquery??浠?

???????? 2.椤甸??腑document.onselectstart=function(event)?芥?版??绂?姝㈤?????涓???瀛?锛?涓昏????ㄩ???╃???跺????瀛?浼??虹?伴???╃???舵??锛?浼???浣??ㄦ?蜂?楠???濡???椤甸??腑?朵??版?规????瑕???瑕?榧?????涓???瀛??????斤?璇ュ?芥?板????琛??规????瑕?澶?????

demo涓?杞藉?板??锛?http://pan.baidu.com/s/1i33yxox

???剧??涓??烘?ユ????锛???瑕?????????浠ヤ?杞?emo????澶??朵唬????琛???寤?emo

继续阅读