复制代码 代码如下: <!--<BR>body{padding-top:50px;padding-left:100px;padding-right:150px;}<BR> .fileDiv{float:left;width:100px;height:100px;text-align:center;line-height:100px;font-size:12px;border:1px solid #cccccc;margin-right:10px;margin-bottom:10px;}<BR> .seled{border:1px solid #ff0000;background-color:#D6DFF7;}<BR>--> // <![CDATA[<BR>Array.prototype.remove = function( item ){<BR> for( var i = 0 ; i < this.length ; i++ ){<BR> if( item == this[i] )<BR> break;<BR> }<BR> if( i == this.length )<BR> return;<BR> for( var j = i ; j < this.length - 1 ; j++ ){<BR> this[ j ] = this[ j + 1 ];<BR> }<BR> this.length--;<BR> } <br><br>String.prototype.replaceAll = function (AFindText,ARepText){ raRegExp = new RegExp(AFindText,"g"); return this.replace(raRegExp,ARepText);}<BR> function getAllChildren(e) {<BR> return e.all ? e.all : e.getElementsByTagName('*');<BR>} <P>document.getElementsBySelector = function(selector) {<BR> if (!document.getElementsByTagName) {<BR> return new Array();<BR> }<BR> var tokens = selector.split(' ');<BR> var currentContext = new Array(document);<BR> for (var i = 0; i < tokens.length; i++) {<BR> token = tokens[i].replace(/^\s+/,'').replace(/\s+$/,'');;<BR> if (token.indexOf('#') > -1) {<BR> var bits = token.split('#');<BR> var tagName = bits[0];<BR> var id = bits[1];<BR> var element = document.getElementById(id);<BR> if (tagName && element.nodeName.toLowerCase() != tagName) {<BR> return new Array();<BR> }<BR> currentContext = new Array(element);<BR> continue; <BR> }<BR> if (token.indexOf('.') > -1) { <P> var bits = token.split('.');<BR> var tagName = bits[0];<BR> var className = bits[1];<BR> if (!tagName) {<BR> tagName = '*';<BR> } <P> var found = new Array;<BR> var foundCount = 0;<BR> for (var h = 0; h < currentContext.length; h++) {<BR> var elements;<BR> if (tagName == '*') {<BR> elements = getAllChildren(currentContext[h]);<BR> } else {<BR> elements = currentContext[h].getElementsByTagName(tagName);<BR> }<BR> for (var j = 0; j < elements.length; j++) {<BR> found[foundCount++] = elements[j];<BR> }<BR> }<BR> currentContext = new Array;<BR> var currentContextIndex = 0;<BR> for (var k = 0; k < found.length; k++) {<BR> if (found[k].className && found[k].className.match(new RegExp('\\b'+className+'\\b'))) {<BR> currentContext[currentContextIndex++] = found[k];<BR> }<BR> }<BR> continue;<BR> }<BR> if (token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/)) {<BR> var tagName = RegExp.$1;<BR> var attrName = RegExp.$2;<BR> var attrOperator = RegExp.$3;<BR> var attrValue = RegExp.$4;<BR> if (!tagName) {<BR> tagName = '*';<BR> }<BR> var found = new Array;<BR> var foundCount = 0;<BR> for (var h = 0; h < currentContext.length; h++) {<BR> var elements;<BR> if (tagName == '*') {<BR> elements = getAllChildren(currentContext[h]);<BR> } else {<BR> elements = currentContext[h].getElementsByTagName(tagName);<BR> }<BR> for (var j = 0; j < elements.length; j++) {<BR> found[foundCount++] = elements[j];<BR> }<BR> }<BR> currentContext = new Array;<BR> var currentContextIndex = 0;<BR> var checkFunction; <BR> switch (attrOperator) {<BR> case '=':<BR> checkFunction = function(e) { return (e.getAttribute(attrName) == attrValue); };<BR> break;<BR> case '~': <BR> checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('\\b'+attrValue+'\\b'))); };<BR> break;<BR> case '|':<BR> checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('^'+attrValue+'-?'))); };<BR> break;<BR> case '^':<BR> checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) == 0); };<BR> break;<BR> case '$': <BR> checkFunction = function(e) { return (e.getAttribute(attrName).lastIndexOf(attrValue) == e.getAttribute(attrName).length - attrValue.length); };<BR> break;<BR> case '*': <BR> checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) > -1); };<BR> break;<BR> default :<BR> checkFunction = function(e) { return e.getAttribute(attrName); };<BR> }<BR> currentContext = new Array;<BR> var currentContextIndex = 0;<BR> for (var k = 0; k < found.length; k++) {<BR> if (checkFunction(found[k])) {<BR> currentContext[currentContextIndex++] = found[k];<BR> }<BR> }<BR> continue;<BR> }<BR> tagName = token;<BR> var found = new Array;<BR> var foundCount = 0;<BR> for (var h = 0; h < currentContext.length; h++) {<BR> var elements = currentContext[h].getElementsByTagName(tagName);<BR> for (var j = 0; j < elements.length; j++) {<BR> found[foundCount++] = elements[j];<BR> }<BR> }<BR> currentContext = found;<BR> }<BR> return currentContext;<BR>}<br><br> function addEvent(eventType,eventFunc,eventObj){<BR> eventObj = eventObj || document;<BR> if(window.attachEvent) eventObj.attachEvent("on"+eventType,eventFunc);<BR> if(window.addEventListener) eventObj.addEventListener(eventType,eventFunc,false);<BR> }<BR>function clearEventBubble(evt){<BR> evt = evt || window.event;<BR> if (evt.stopPropagation) evt.stopPropagation(); else evt.cancelBubble = true; <BR> if (evt.preventDefault) evt.preventDefault(); else evt.returnValue = false;<BR>}<br><br>function posXY(event){<BR> event = event || window.event;<BR> var posX = event.pageX || (event.clientX +<BR> (document.documentElement.scrollLeft || document.body.scrollLeft));<BR> var posY = event.pageY || (event.clientY +<BR> (document.documentElement.scrollTop || document.body.scrollTop));<BR> return {x:posX, y:posY};<BR>} <P> var _selectedRegions = [];<BR> function RegionSelect(selRegionProp){<BR> this.regions =[];<BR> var _regions = document.getElementsBySelector(selRegionProp["region"]);<BR> if(_regions && _regions.length > 0){<BR> var _self = this;<BR> for(var i=0; i< _regions.length;i++){<BR> _regions[i].onmousedown = function(){<BR> var evt = window.event || arguments[0];<BR> if(!evt.shiftKey && !evt.ctrlKey){<BR> // 清空所有select样式<BR> _self.clearSelections(_regions);<BR> this.className += " "+_self.selectedClass;<BR> // 清空selected数组,并加入当前select中的元素<BR> _selectedRegions = [];<BR> _selectedRegions.push(this);<BR> }else{<BR> if(this.className.indexOf(_self.selectedClass) == -1){<BR> this.className += " "+_self.selectedClass;<BR> _selectedRegions.push(this);<BR> }else{<BR> this.className = this.className.replaceAll(_self.selectedClass,"");<BR> _selectedRegions.remove(this);<BR> }<BR> }<BR> clearEventBubble(evt);<BR> }<BR> this.regions.push(_regions[i]);<BR> }<BR> }<BR> this.selectedClass = selRegionProp["selectedClass"];<BR> this.selectedRegion = [];<BR> this.selectDiv = null;<BR> this.startX = null;<BR> this.startY = null;<BR> }<br><br> RegionSelect.prototype.select = function(){<BR> var _self = this;<BR> addEvent("mousedown",function(){<BR> var evt = window.event || arguments[0];<BR> _self.onBeforeSelect(evt);<BR> clearEventBubble(evt);<BR> },document);<br><br> addEvent("mousemove",function(){<BR> var evt = window.event || arguments[0];<BR> _self.onSelect(evt);<BR> clearEventBubble(evt);<BR> },document);<br><br> addEvent("mouseup",function(){<BR> _self.onEnd();<BR> },document);<BR> }<br><br> RegionSelect.prototype.onBeforeSelect = function(evt){<BR> if(!document.getElementById("selContainer")){<BR> this.selectDiv = document.createElement("div");<BR> this.selectDiv.style.cssText = "position:absolute;width:0px;height:0px;font-size:0px;margin:0px;padding:0px;border:1px dashed #0099FF;background-color:#C3D5ED;z-index:1000;filter:alpha(opacity:60);opacity:0.6;display:none;";<BR> this.selectDiv.id = "selContainer";<BR> document.body.appendChild(this.selectDiv);<BR> }else{<BR> this.selectDiv = document.getElementById("selContainer");<BR> }<br><br> this.startX = posXY(evt).x;<BR> this.startY = posXY(evt).y;<BR> this.isSelect = true;<br><br> }<br><br> RegionSelect.prototype.onSelect = function(evt){<BR> var _self = this;<BR> if(_self.isSelect){<BR> if(_self.selectDiv.style.display == "none") _self.selectDiv.style.display = "";<br><br> var posX = posXY(evt).x;<BR> var poxY = posXY(evt).y;<br><br> _self.selectDiv.style.left = Math.min(posX, this.startX);<BR> _self.selectDiv.style.top = Math.min(poxY, this.startY);<BR> _self.selectDiv.style.width = Math.abs(posX - this.startX);<BR> _self.selectDiv.style.height = Math.abs(poxY - this.startY); <br><br> var regionList = _self.regions;<BR> for(var i=0; i< regionList.length; i++){<BR> var r = regionList[i], sr = _self.innerRegion(_self.selectDiv,r);<BR> if(sr && r.className.indexOf(_self.selectedClass) == -1){<BR> r.className = r.className + " "+_self.selectedClass; <BR> _selectedRegions.push(r);<BR> }else if(!sr && r.className.indexOf(_self.selectedClass) != -1){<BR> r.className = r.className.replaceAll(_self.selectedClass,"");<BR> _selectedRegions.remove(r);<BR> }<br><br> }<BR> }<BR> }<br><br> RegionSelect.prototype.onEnd = function(){<BR> if(this.selectDiv){<BR> this.selectDiv.style.display = "none"; <BR> }<BR> this.isSelect = false;<BR> //_selectedRegions = this.selectedRegion;<BR> }<br><br> // 判断一个区域是否在选择区内<BR> RegionSelect.prototype.innerRegion = function(selDiv, region){<BR> var s_top = parseInt(selDiv.style.top);<BR> var s_left = parseInt(selDiv.style.left);<BR> var s_right = s_left + parseInt(selDiv.offsetWidth);<BR> var s_bottom = s_top + parseInt(selDiv.offsetHeight);<br><br> var r_top = parseInt(region.offsetTop);<BR> var r_left = parseInt(region.offsetLeft);<BR> var r_right = r_left + parseInt(region.offsetWidth);<BR> var r_bottom = r_top + parseInt(region.offsetHeight);<br><br> var t = Math.max(s_top, r_top);<BR> var r = Math.min(s_right, r_right);<BR> var b = Math.min(s_bottom, r_bottom);<BR> var l = Math.max(s_left, r_left);<br><br> if (b > t+5 && r > l+5) {<BR> return region;<BR> } else {<BR> return null;<BR> }<br><br> }<br><br> RegionSelect.prototype.clearSelections = function(regions){<BR> for(var i=0; i<regions.length;i++){<BR> regions[i].className = regions[i].className.replaceAll(this.selectedClass,"");<BR> }<BR> }<br><br> function getSelectedRegions(){<BR> return _selectedRegions;<BR> } <P> function showSelDiv(){<BR> var selInfo = "";<BR> var arr = getSelectedRegions();<BR> for(var i=0; i<arr.length;i++){<BR> selInfo += arr[i].innerHTML+"\n";<BR> } <P> alert("共选择 "+arr.length+" 个文件,分别是:\n"+selInfo);<br><br> }<BR>// ]]>getRegions file1 file2 file3 file4 file5 file6 file7 file8 getRegions // <![CDATA[<BR>new RegionSelect({<BR> region:'div.fileDiv',<BR> selectedClass: 'seled'<BR> }).select();<BR>// ]]>