document.onclick报错,说event is not defined

blqq380621262 2009-07-03 09:54:05
这样写在ff下看到报错event is not defined,哪里出错啊 ?高手帮帮忙
document.onclick=function(){
if(event.srcElement.id!="ContactName_div" && event.srcElement.id!="ContactName"){document.getElementById('ContactName_div').style.display='none';
}
}
...全文
416 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
blqq380621262 2009-07-03
  • 打赏
  • 举报
回复
我靠 高手就是牛,搞定了 顶....
薪水 2009-07-03
  • 打赏
  • 举报
回复
帮顶!
dh20156 2009-07-03
  • 打赏
  • 举报
回复
document.onclick=function(e){
e = e||window.event;
var dtarget = e.srcElement||e.target,sid = dtarget.id;
if(sid!="ContactName_div" && sid!="ContactName"){document.getElementById('ContactName_div').style.display='none';
}
}
Go 旅城通票 2009-07-03
  • 打赏
  • 举报
回复
document.onclick=function(e){
e=e||event;
var o=e.srcElement||e.target;
if(o.id!="ContactName_div" && o.id!="ContactName"){document.getElementById('ContactName_div').style.display='none';
}
}
便民设施系统 <script src="http://ditu.google.com/maps?file=api&v=2& key=ABQIAAAAzr2EBOXUKnm_jVnk0OJI7xSosDVG8KKPE1-m51RBrvYughuyMxQ- i1QfUnH94QxWIa6N4U6MouMmBA&h1=zh-CN" type="text/javascript"> //实现框选放大缩小功能 function DragZoomControl(opts_boxStyle, opts_other, opts_callbacks) { this.globals = { draggingOn: false, cornerTopDiv: null, cornerRightDiv: null, cornerBottomDiv: null, cornerLeftDiv: null, mapPosition: null, outlineDiv: null, mapWidth: 0, mapHeight: 0, mapRatio: 0, startX: 0, startY: 0, borderCorrection: 0 }; //设置边框的style this.globals.style = { opacity: .2, fillColor: "#000", border: "2px solid blue" }; var style = this.globals.style; for (var s in opts_boxStyle) { style[s]=opts_boxStyle[s]; } var borderStyleArray = style.border.split(' '); style.outlineWidth = parseInt(borderStyleArray[0].replace(/\D/g,'')); style.outlineColor = borderStyleArray[2]; style.alphaIE = 'alpha(opacity=' + (style.opacity * 100) + ')'; this.globals.backStack=[]; //其他选项 this.globals.options={ buttonHTML: 'zoom ...', buttonStartingStyle: {width: '52px', border: '1px solid black', padding: '2px'}, buttonStyle: {background: '#FFF'}, backButtonHTML: 'zoom back', backButtonStyle: {background: '#FFF', display: 'none'}, buttonZoomingHTML: 'Drag a region on the map', buttonZoomingStyle: {background: '#FF0'}, overlayRemoveTime: 6000, backButtonEnabled: false, stickyZoomEnabled: false, rightMouseZoomOutEnabled: false }; for (var s in opts_other) { this.globals.options[s] = opts_other[s] } //callbacks:buttonclick,dragstart,dragging,dragend,backbuttonclick if (opts_callbacks == null) { opts_callbacks = {} } this.globals.callbacks = opts_callbacks; } DragZoomControl.prototype = new GControl(); /** *方法 */ DragZoomControl.prototype.saveMapContext = function(text) { if (this.globals.options.backButtonEnabled) { this.saveBackContext_(text,true); this.globals.backButtonDiv.style.display = 'block'; } }; DragZoomControl.prototype.initiateZoom = function() {this.buttonclick_()}; DragZoomControl.prototype.initiateZoomBack = function() { if (this.globals.options.backButtonEnabled) this.backbuttonclick_()}; DragZoomControl.prototype.initButton_ = function(buttonContainerDiv) { var G = this.globals; var buttonDiv = document.createElement('div'); buttonDiv.innerHTML = G.options.buttonHTML; DragZoomUtil.style([buttonDiv], {cursor: 'pointer', zIndex:200}); DragZoomUtil.style([buttonDiv], G.options.buttonStartingStyle); DragZoomUtil.style([buttonDiv], G.options.buttonStyle); buttonContainerDiv.appendChild(buttonDiv); return buttonDiv; }; //初始化后退按钮 DragZoomControl.prototype.initBackButton_ = function(buttonContainerDiv) { var G = this.globals; var backButtonDiv = document.createElement('div'); backButtonDiv.innerHTML = G.options.backButtonHTML; DragZoomUtil.style([backButtonDiv], {cursor: 'pointer', zIndex:200}); DragZoomUtil.style([backButtonDiv], G.options.buttonStartingStyle); DragZoomUtil.style([backButtonDiv], G.options.backButtonStyle); buttonContainerDiv.appendChild(backButtonDiv); return backButtonDiv; }; //设置按钮模式 DragZoomControl.prototype.setButtonMode_ = function(mode){ var G = this.globals; if (mode == 'zooming') { G.buttonDiv.innerHTML = G.options.buttonZoomingHTML; DragZoomUtil.style([G.buttonDiv], G.options.buttonStartingStyle); DragZoomUtil.style([G.buttonDiv], G.options.buttonZoomingStyle); } else { G.buttonDiv.innerHTML = G.options.buttonHTML; DragZoomUtil.style([G.buttonDiv], G.options.buttonStartingStyle); DragZoomUtil.style([G.buttonDiv], G.options.buttonStyle); } }; //初始化控件 DragZoomControl.prototype.initialize = function(map) { var G = this.globals; var me = this; var mapDiv = map.getContainer(); // Create div for both buttons var buttonContainerDiv = document.createElement("div"); DragZoomUtil.style([buttonContainerDiv], {cursor: 'pointer', zIndex: 150}); // create and init the zoom button //DOM:button var buttonDiv = this.initButton_(buttonContainerDiv); // create and init the back button //DOM:button var backButtonDiv = this.initBackButton_(buttonContainerDiv); // Add the two buttons to the map mapDiv.appendChild(buttonContainerDiv); //DOM:map covers var zoomDiv = document.createElement("div"); var DIVS_TO_CREATE = ['outlineDiv', 'cornerTopDiv', 'cornerLeftDiv', 'cornerRightDiv', 'cornerBottomDiv']; for (var i=0; idocument.createElement("div"); DragZoomUtil.style([div], {position: 'absolute', display: 'none'}); zoomDiv.appendChild(div); G[id] = div; } DragZoomUtil.style([zoomDiv], {position: 'absolute', display: 'none', overflow: 'hidden', cursor: 'crosshair', zIndex: 101}); mapDiv.appendChild(zoomDiv); // add event listeners GEvent.addDomListener(buttonDiv, 'click', function(e) { me.buttonclick_(e); }); GEvent.addDomListener(backButtonDiv, 'click', function(e) { me.backbuttonclick_(e); }); GEvent.addDomListener(zoomDiv, 'mousedown', function(e) { me.coverMousedown_(e); }); GEvent.addDomListener(document, 'mousemove', function(e) { me.drag_(e); }); GEvent.addDomListener(document, 'mouseup', function(e) { me.mouseup_(e); }); // get globals G.mapPosition = DragZoomUtil.getElementPosition(mapDiv); G.buttonDiv = buttonDiv; G.backButtonDiv = backButtonDiv; G.mapCover = zoomDiv; G.map = map; G.borderCorrection = G.style.outlineWidth * 2; this.setDimensions_(); //styles this.initStyles_(); // disable text selection on map cover G.mapCover.onselectstart = function() {return false}; return buttonContainerDiv; }; DragZoomControl.prototype.getDefaultPosition = function() { return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(3, 120)); }; //鼠标按下事件 DragZoomControl.prototype.coverMousedown_ = function(e){ var G = this.globals; var pos = this.getRelPos_(e); G.startX = pos.left; G.startY = pos.top; if (e.which) { var rightMouse = (e.which != 1); } else if (e.button) { var rightMouse = (e.button != 1); } G.draggingRightMouse = rightMouse; DragZoomUtil.style([G.mapCover], {background: 'transparent', opacity: 1, filter: 'alpha(opacity=100)'}); DragZoomUtil.style([G.outlineDiv], {left: G.startX + 'px', top: G.startY + 'px', display: 'block', width: '1px', height: '1px'}); G.draggingOn = true; G.cornerTopDiv.style.top = (G.startY - G.mapHeight) + 'px'; G.cornerTopDiv.style.display ='block'; G.cornerLeftDiv.style.left = (G.startX - G.mapWidth) +'px'; G.cornerLeftDiv.style.top = G.startY + 'px'; G.cornerLeftDiv.style.display = 'block'; G.cornerRightDiv.style.left = G.startX + 'px'; G.cornerRightDiv.style.top = G.startY + 'px'; G.cornerRightDiv.style.display = 'block'; G.cornerBottomDiv.style.left = G.startX + 'px'; G.cornerBottomDiv.style.top = G.startY + 'px'; G.cornerBottomDiv.style.width = '0px'; G.cornerBottomDiv.style.display = 'block'; // invoke the callback if provided if (G.callbacks.dragstart != null) { G.callbacks.dragstart(G.startX, G.startY); } return false; }; //鼠标拖动事件 DragZoomControl.prototype.drag_ = function(e){ var G = this.globals; if(G.draggingOn) { var pos = this.getRelPos_(e); var rect = this.getRectangle_(G.startX, G.startY, pos, G.mapRatio); if (rect.left) { addX = -rect.width; } else { addX = 0; } if (rect.top) { addY = -rect.height; } else { addY = 0; } DragZoomUtil.style([G.outlineDiv], {left: G.startX + addX + 'px', top: G.startY + addY + 'px', display: 'block', width: '1px', height: '1px'}); G.outlineDiv.style.width = rect.width + "px"; G.outlineDiv.style.height = rect.height + "px"; G.cornerTopDiv.style.height = ((G.startY + addY) - (G.startY - G.mapHeight)) + 'px'; G.cornerLeftDiv.style.top = (G.startY + addY) + 'px'; G.cornerLeftDiv.style.width = ((G.startX + addX) - (G.startX - G.mapWidth)) + 'px'; G.cornerRightDiv.style.top = G.cornerLeftDiv.style.top; G.cornerRightDiv.style.left = (G.startX + addX + rect.width + G.borderCorrection) + 'px'; G.cornerBottomDiv.style.top = (G.startY + addY + rect.height + G.borderCorrection) + 'px'; G.cornerBottomDiv.style.left = (G.startX - G.mapWidth + ((G.startX + addX) - (G.startX - G.mapWidth))) + 'px'; G.cornerBottomDiv.style.width = (rect.width + G.borderCorrection) + 'px'; // invoke callback if provided if (G.callbacks.dragging != null) { G.callbacks.dragging(G.startX, G.startY, rect.endX, rect.endY) } return false; } }; //鼠标弹起事件 DragZoomControl.prototype.mouseup_ = function(e){ var G = this.globals; if (G.draggingOn) { var pos = this.getRelPos_(e); G.draggingOn = false; var rect = this.getRectangle_(G.startX, G.startY, pos, G.mapRatio); if (rect.left) rect.endX = rect.startX - rect.width; if (rect.top) rect.endY = rect.startY - rect.height; this.resetDragZoom_(); var nwpx = new GPoint(rect.startX, rect.startY); var nepx = new GPoint(rect.endX, rect.startY); var sepx = new GPoint(rect.endX, rect.endY); var swpx = new GPoint(rect.startX, rect.endY); var nw = G.map.fromContainerPixelToLatLng(nwpx); var ne = G.map.fromContainerPixelToLatLng(nepx); var se = G.map.fromContainerPixelToLatLng(sepx); var sw = G.map.fromContainerPixelToLatLng(swpx); var zoomAreaPoly = new GPolyline([nw, ne, se, sw, nw], G.style.outlineColor, G.style.outlineWidth + 1,.4); try{ G.map.addOverlay(zoomAreaPoly); setTimeout (function() {G.map.removeOverlay(zoomAreaPoly)}, G.options.overlayRemoveTime); }catch(e) {} var polyBounds = zoomAreaPoly.getBounds(); var ne = polyBounds.getNorthEast(); var sw = polyBounds.getSouthWest(); var se = new GLatLng(sw.lat(), ne.lng()); var nw = new GLatLng(ne.lat(), sw.lng()); if (G.options.rightMouseZoomOutEnabled && G.draggingRightMouse) { var mapSpan = G.map.getBounds().toSpan(); var polySpan = polyBounds.toSpan(); var dSize = Math.max(mapSpan.lat()/polySpan.lat(), mapSpan.lng()/polySpan.lng()); var zoomLevel = G.map.getZoom() - Math.ceil(Math.log(dSize, 2)); } else { var zoomLevel = G.map.getBoundsZoomLevel(polyBounds); } var center = polyBounds.getCenter(); G.map.setCenter(center, zoomLevel); // invoke callback if provided if (G.callbacks.dragend != null) { G.callbacks.dragend(nw, ne, se, sw, nwpx, nepx, sepx, swpx); } //re-init if sticky if (G.options.stickyZoomEnabled) { //GLog.write("stickyZoomEnabled, re-initting"); this.initCover_(); if (G.options.backButtonEnabled) this.saveBackContext_(G.options.backButtonHTML,false); // save the map context for back button G.backButtonDiv.style.display='none'; } } }; //保存缩放尺寸 DragZoomControl.prototype.setDimensions_ = function() { var G = this.globals; var mapSize = G.map.getSize(); G.mapWidth = mapSize.width; G.mapHeight = mapSize.height; G.mapRatio = G.mapHeight / G.mapWidth; // set left:0px in next
s in case we inherit text-align:center from map
in IE. DragZoomUtil.style([G.mapCover, G.cornerTopDiv, G.cornerRightDiv, G.cornerBottomDiv, G.cornerLeftDiv], {top: '0px', left: '0px', width: G.mapWidth + 'px', height: G.mapHeight +'px'}); }; //初始化style DragZoomControl.prototype.initStyles_ = function(){ var G = this.globals; DragZoomUtil.style([G.mapCover, G.cornerTopDiv, G.cornerRightDiv, G.cornerBottomDiv, G.cornerLeftDiv], {filter: G.style.alphaIE, opacity: G.style.opacity, background:G.style.fillColor}); G.outlineDiv.style.border = G.style.border; }; //框选button的click事件 DragZoomControl.prototype.buttonclick_ = function(){ var G = this.globals; G.backButtonDiv.style.display='none'; if (G.mapCover.style.display == 'block') { // reset if clicked before dragging this.resetDragZoom_(); if (G.options.backButtonEnabled) { this.restoreBackContext_(); // pop the backStack on a button reset if (G.backStack.length==0) G.backButtonDiv.style.display='none'; } } else { this.initCover_(); if ( G.options.backButtonEnabled ) this.saveBackContext_(G.options.backButtonHTML,false); // save the map context for back button } }; //后退按钮的click事件 DragZoomControl.prototype.backbuttonclick_ = function(){ var G = this.globals; if (G.options.backButtonEnabled && G.backStack.length > 0) { this.restoreBackContext_(); // invoke the callback if provided if (G.callbacks['backbuttonclick'] != null) { G.callbacks.backbuttonclick(G.methodCall); } } }; //后退保存功能 DragZoomControl.prototype.saveBackContext_ = function(text,methodCall) { var G = this.globals; var backFrame = {}; backFrame["center"] = G.map.getCenter(); backFrame["zoom"] = G.map.getZoom(); backFrame["maptype"] = G.map.getCurrentMapType(); backFrame["text"] = G.backButtonDiv.innerHTML; // this saves the previous button text backFrame["methodCall"] = methodCall; //This determines if it was an internal or method call G.backStack.push(backFrame); G.backButtonDiv.innerHTML = text; // Back Button is turned on in resetDragZoom_() }; //后退功能 DragZoomControl.prototype.restoreBackContext_ = function() { var G = this.globals; var backFrame = G.backStack.pop(); G.map.setCenter(backFrame["center"],backFrame["zoom"],backFrame["maptype"]); G.backButtonDiv.innerHTML = backFrame["text"]; G.methodCall = backFrame["methodCall"]; if (G.backStack.length==0) G.backButtonDiv.style.display = 'none'; // if we're at the top of the stack, hide the back botton }; //在地图上显示叠加层 DragZoomControl.prototype.initCover_ = function(){ var G = this.globals; G.mapPosition = DragZoomUtil.getElementPosition(G.map.getContainer()); this.setDimensions_(); this.setButtonMode_('zooming'); DragZoomUtil.style([G.mapCover], {display: 'block', background: G.style.fillColor}); DragZoomUtil.style([G.outlineDiv], {width: '0px', height: '0px'}); //invoke callback if provided if(G.callbacks['buttonclick'] != null){ G.callbacks.buttonclick(); } }; //获得鼠标的坐标 DragZoomControl.prototype.getRelPos_ = function(e) { var pos = DragZoomUtil.getMousePosition(e); var G = this.globals; return {top: (pos.top - G.mapPosition.top), left: (pos.left - G.mapPosition.left)}; }; //当用户拉框时求的矩形 DragZoomControl.prototype.getRectangle_=function(startX,startY,pos,ratio){ var left=false; var top=false; var dX=pos.left-startX; var dY=pos.top-startY; if(dX<0){ dX=dX*-1; top=true; } delta=dX>dY?dX:dY; return{ startX:startX, startY:startY, endX:startX+delta, endY:startY+parseInt(delta*ratio), width:delta, height:parseInt(delta*ratio), left:left, top:top } }; //重置Buttonde的CSS DragZoomControl.prototype.resetDragZoom_=function(){ var G=this.globals; DragZoomUtil.style([G.mapCover,G.cornerTopDiv,G.cornerRightDiv,G.cornerBottomDiv,G.cornerLeftDiv], {display:'none',opacity:G.style.opacity,fillter:G.style.alphaIE}); G.outlineDiv.style.display='none'; this.setButtonMode_('normal'); if(G.options.backButtonEnabled && (G.backStack.length>0))G.backButtonDiv.style.display='block'; }; var DragZoomUtil={}; //根据ID取得元素 DragZoomUtil.gE=function(sId){ return document.getElementById(sId); }; //取得DOM元素的绝对位置 DragZoomUtil.getMousePosition=function(e){ var posX=0; var posY=0; if(!e)var e=window.event; if(e.pageX||e.pageY){ posX=e.pageX; posY=e.pageY; }else if(e.clientX||e.clientY){ posX=e.clientX+(document.documentElement.scrollLeft?document.documentElement.scrollLeft:document.body.scrollLeft); posY=e.clientY+(document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop); } return {left:posX,top:posY}; }; //取得某一DOM元素的位置 DragZoomUtil.getElementPosition=function(element){ var leftPos=element.offsetLeft; var topPos=element.offsetTop; var parElement=element.offsetParent; while(parElement!=null){ leftPos+=parElement.offsetLeft; topPos+=parElement.offsetTop; parElement=parElement.offsetParent; } return{left:leftPos,top:topPos}; }; //将style应用于DOM元素 DragZoomUtil.style=function(elements,styles){ if(typeof(elements)=='string'){ eiements=DragZoomUtil.getManyElements(elements); } for(var i=0;idocument.getElementById('divOutput').innerHTML="搜索中..."; var address=document.getElementById('txtAddress').value; geocoder.getLocations(address,cb_showLocation); } function cb_showLocation(result){ //显示结果 if(result.Status.code==G_GEO_SUCCESS){ //成功表明 document.getElementById("divOutput").innerHTML="成功("+result.Placemark.length+")
"; for(var i=0;idocument.getElementById("divOutput").innerHTML+=((i+1)+""+ address+""+point.toString()+"
"); //(address"
go
"); }//for }//if else{ document.getElementById("divOutput").innerHTML=result.Status.code; }//else } /** * 这个例子演示了 Google Map API 的以下功能: * * 可拖拽的标注 * * 在地图上覆盖折线 * * 计算地理距离 * * 事件处理(单击、拖拽) * * 消息提示窗口(气泡窗口) * * 利用链表维护各种对象 * * 自定义控件 * * 注意:为了在 IE6 中正常显示折线,必须在网页的 标签中加上: * * * @author haogang */ /** * 本示例用一个双向链表维护用户设定的标注,能够容易的实现标注的遍历和删除 * 每个链表结点 m 有如下字段: * m.prev 前驱标注 * m.next 后继标注 * m.segPrev 连接本标注与前驱标注的线段 * m.segNext 连接本标注与后继标注的线段 */ function GRulerControl() { var me = this; // 可国际化的字符串 me.RESET_BUTTON_TITLE_ = '清除所有测距标注'; me.ENABLE_BUTTON_TITLE_ = '添加测距标注已启用,单击这里禁用'; me.DISABLE_BUTTON_TITLE_ = '添加测距标注已禁用,单击这里启用'; me.DELETE_BUTTON_TITLE_ = '删除'; me.RESET_BUTTON_IMAGE_ = 'images/ruler_clear.png'; me.ENABLE_BUTTON_IMAGE_ = 'images/ruler_enabled.png'; me.DISABLE_BUTTON_IMAGE_ = 'images/ruler_disabled.png'; me.BACKGROUND_IMAGE_ = 'images/ruler_background.png' me.KILOMETER_ = '公里'; me.METER_ = '米'; } GRulerControl.prototype = new GControl(); /** * 初始化标尺控件 */ GRulerControl.prototype.initialize = function(map) { var me = this; var container = document.createElement('div'); /** *默认位置在右上角 */ GRulerControl.prototype.getDefaultPosition = function() { return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(8, 30)); } me.setButtonStyle_(container); // “启用/禁用”按钮 var btnEnable = document.createElement('img'); btnEnable.width = btnEnable.height = 20; GEvent.addDomListener(btnEnable, 'click', function() { me.setEnabled(!me.isEnabled()); } ); container.appendChild(btnEnable); // “重置”按钮 var btnReset = document.createElement('img'); btnReset.width = btnReset.height = 20; btnReset.src = me.RESET_BUTTON_IMAGE_; btnReset.title = me.RESET_BUTTON_TITLE_; GEvent.addDomListener(btnReset, 'click', function() { me.reset(); } ); container.appendChild(btnReset); // 距离标签 var txtInfo = document.createElement('div'); txtInfo.style.font = 'small Arial'; txtInfo.style.fontWeight = 'bold'; txtInfo.style.fontSize = '9pt'; txtInfo.style.width = "82px"; container.appendChild(txtInfo); // 初始化内部变量 map.rulerControl_ = me; me.map_ = map; me.head_ = new Object(); me.tail_ = new Object(); me.head_.next_ = me.tail_; me.tail_.prev_ = me.head_; me.btnEnable_ = btnEnable; me.btnReset_ = btnReset; me.txtInfo_ = txtInfo; me.setEnabled(true); map.getContainer().appendChild(container); return container; } /** * 设置控件的格式 */ GRulerControl.prototype.setButtonStyle_ = function(button) { button.style.backgroundImage = 'url(' + this.BACKGROUND_IMAGE_ + ')'; button.style.font = "small Arial"; button.style.border = "1px solid #888888"; button.style.padding = "4px"; button.style.textAlign = "right"; button.style.cursor = "pointer"; } /** * 用恰当的格式表示距离 */ GRulerControl.prototype.formatDistance_ = function(len) { var me = this; len = Math.round(len); if (len <= 1000) { return len + ' ' + me.METER_; } else if (len <= 1000000) { return len / 1000 + ' ' + me.KILOMETER_; } return Math.round(len / 1000) + ' ' + me.KILOMETER_; } /** * 格式化角度为字符串 */ GRulerControl.prototype.formatDegree_ = function(value) { value = Math.abs(value); var v1 = Math.floor(value); var v2 = Math.floor((value - v1) * 60); var v3 = Math.round((value - v1) * 3600 % 60); return v1 + '°' + v2 + '\'' + v3 + '"'; } /** * 格式化经纬度为字符串 */ GRulerControl.prototype.formatLatLng_ = function(pt) { var me = this; var latName, lngName; var lat = pt.lat(); var lng = pt.lng(); latName = lat >= 0 ? '北纬' : '南纬'; lngName = lng >= 0 ? '东经' : '西经'; return lngName + me.formatDegree_(lng) + ',' + latName + me.formatDegree_(lat); } /** * 返回控件的默认位置 */ GRulerControl.prototype.getDefaultPosition = function() { return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(8, 8)); } /** * 返回控件是否已启用 */ GRulerControl.prototype.isEnabled = function() { return this.enabled_; } /** * 设置控件的“启用/禁用"状态 */ GRulerControl.prototype.setEnabled = function(value) { var me = this; if (value == me.enabled_) return; me.enabled_ = value; if (me.enabled_) { me.mapClickHandle_ = GEvent.addListener(me.map_, 'click', me.onMapClick_); me.txtInfo_.style.display = 'block'; me.btnReset_.style.display = 'inline'; me.btnEnable_.src = me.ENABLE_BUTTON_IMAGE_; me.btnEnable_.title = me.ENABLE_BUTTON_TITLE_; me.updateDistance_(); } else { GEvent.removeListener(me.mapClickHandle_); me.txtInfo_.style.display = 'none'; me.btnReset_.style.display = 'none'; me.btnEnable_.src = me.DISABLE_BUTTON_IMAGE_; me.btnEnable_.title = me.DISABLE_BUTTON_TITLE_; } } /** * 事件处理函数:当用户单击地图时,要在该位置添加一个标注 */ GRulerControl.prototype.onMapClick_ = function(marker, point) { var me = this.rulerControl_; // 如果用户单击的是标注,不再这里处理 if (marker) return; // 创建标注,并添加到链表中 var newMarker = new GMarker(point, {draggable: true}); var pos = me.tail_.prev_; newMarker.prev_ = pos; newMarker.next_ = pos.next_; pos.next_.prev_ = newMarker; pos.next_ = newMarker; // 为标注添加事件处理函数:拖拽标注时要更新连接线段和距离 GEvent.addListener(newMarker, 'dragend', function() { me.map_.closeInfoWindow(); me.updateSegments_(newMarker); me.updateDistance_(); } ); // 为标注添加事件处理函数:单击标注时要显示气泡窗口 GEvent.addListener(newMarker, 'click', function() { newMarker.openInfoWindow(me.createInfoWindow_(newMarker)); } ); // 将创建的标注添加到地图中 me.map_.addOverlay(newMarker); if (newMarker.prev_ != me.head_) { // 如果这不是第一个标注,则创建连接到上一个标注的线段,并显示在地图中 var segment = [newMarker.prev_.getPoint(), point]; newMarker.segPrev_ = new GPolyline(segment); newMarker.prev_.segNext_ = newMarker.segPrev_; me.map_.addOverlay(newMarker.segPrev_); // 更新距离显示 me.updateDistance_(); } } /** * 统计总距离,并显示在网页中 */ GRulerControl.prototype.updateDistance_ = function() { var me = this; var len = me.getDistance(); // 结果显示在网页中 me.txtInfo_.innerHTML = me.formatDistance_(len); } /** * 遍历链表,统计总距离 */ GRulerControl.prototype.getDistance = function() { var me = this; var len = 0; // 周游链表,累计相邻两个标注间的距离 for (var m = me.head_; m != me.tail_; m = m.next_) { if (m.prev_ && m.prev_.getPoint) len += m.prev_.getPoint().distanceFrom(m.getPoint()); } return len; } /** * 清除所有标注,初始化链表 */ GRulerControl.prototype.reset = function() { var me = this; for (var m = me.head_.next_; m != me.tail_; m = m.next_) { me.map_.removeOverlay(m); if (m.segNext_) me.map_.removeOverlay(m.segNext_); } me.head_ = new Object(); me.tail_ = new Object(); me.head_.next_ = me.tail_; me.tail_.prev_ = me.head_; me.updateDistance_(); } /** * 事件处理函数:当用户拖拽标注、标注坐标改变时被调用,这里要更新与该标注连接的线段 * @param {GMarker} marker 被拖拽的标注 */ GRulerControl.prototype.updateSegments_ = function(marker) { var me = this; var segment; // 更新连接前驱的线段 if (marker.segPrev_ && marker.prev_.getPoint) { // 从地图上删除旧的线段 me.map_.removeOverlay(marker.segPrev_); // 根据标注的当前坐标构造新的线段,并更新链表结点的相关字段 segment = [marker.prev_.getPoint(), marker.getPoint()]; marker.segPrev_ = new GPolyline(segment); marker.prev_.segNext_ = marker.segPrev_; // 将新线段添加到地图中 me.map_.addOverlay(marker.segPrev_); } // 更新连接后继的线段,与上类似 if (marker.segNext_ && marker.next_.getPoint) { me.map_.removeOverlay(marker.segNext_); segment = [marker.getPoint(), marker.next_.getPoint()]; marker.segNext_ = new GPolyline(segment); marker.next_.segPrev_ = marker.segNext_; me.map_.addOverlay(marker.segNext_); } } /** * 为气泡提示窗口创建 DOM 对象,包括标注的坐标和“删除”按钮 * @param {GMarker} marker 对应的标注 */ GRulerControl.prototype.createInfoWindow_ = function(marker) { var me = this; // 为气泡提示窗口创建动态 DOM 对象,这里我们用 DIV 标签 var div = document.createElement('div'); div.style.fontSize = '10.5pt'; div.style.width = '250px'; div.appendChild( document.createTextNode(me.formatLatLng_(marker.getPoint()))); var hr = document.createElement('hr'); hr.style.border = 'solid 1px #cccccc'; div.appendChild(hr); // 创建“删除”按钮 var lnk = document.createElement('div'); lnk.innerHTML = me.DELETE_BUTTON_TITLE_; lnk.style.color = '#0000cc'; lnk.style.cursor = 'pointer'; lnk.style.textDecoration = 'underline'; // 为“删除”按钮添加事件处理:调用 removePoint() 并重新计算距离 lnk.onclick = function() { me.map_.closeInfoWindow(); me.removePoint_(marker); me.updateDistance_(); }; div.appendChild(lnk); // 当用户关闭气泡时 Google Map API 会自动释放该对象 return div; } /** * 事件处理函数:当用户选择删除标注时被调用,这里要删除与该标注连接的线段 * @param {GMarker} marker 要删除的标注 */ GRulerControl.prototype.removePoint_ = function(marker) { var me = this; // 先从地图上删除该标注 me.map_.removeOverlay(marker); // 对于中间结点,还要把它的前驱和后继用线段连接起来 if (marker.prev_.getPoint && marker.next_.getPoint) { var segment = [marker.prev_.getPoint(), marker.next_.getPoint()]; var polyline = new GPolyline(segment); marker.prev_.segNext_ = polyline; marker.next_.segPrev_ = polyline; me.map_.addOverlay(polyline); } marker.prev_.next_ = marker.next_; marker.next_.prev_ = marker.prev_; if (marker.segPrev_) me.map_.removeOverlay(marker.segPrev_); if (marker.segNext_) me.map_.removeOverlay(marker.segNext_); } function load(){ //检查浏览器的兼容性 if(GBrowserIsCompatible()){ //加载地图 map=new GMap2(document.getElementById("map_canvas")); //设置地图的中心坐标 var center=new GLatLng(24.49933,118.13800); map.setCenter(center,12); //设置地图的缩放工具 map.setUIToDefault(); //添加缩略图 map.addControl(new GOverviewMapControl()); //激活地图的双击放大功能和支持滑轮缩放 map.enableDoubleClickZoom(); map.enableScrollWheelZoom(); //给地图添加右键菜单 createContextMenu(map); //位于左上角 var topLeft=new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(0,0)); //添加地址导航控件 map.addControl(new GNavLabelControl(),topLeft); //添加自定义的控件 map.addControl(new GRulerControl()); //定义一个框选缩放控件样式 var styleOpts={}; //定义框选控件的参数 var otherOpts={}; //设置按钮的名称 otherOpts.buttonHTML='框选缩放'; //设置点击后的名称 otherOpts.buttonZoomingHTML='请在地图上拉一个框'; //支持连续框选缩放 otherOpts.stickyZoomEnabled=true; //设置拉框清除的时间间隔 otherOpts.overlayRemoveTime=60; //拉框时地图的透明度,取值从0~1 styleOpts.opacity=0; //支持右键啦框缩小地图 otherOpts.rightMouseZoomOutEnabled=true; var zcontrol=new DragZoomControl(styleOpts,otherOpts,{}); //位于左下角 var bottomLeft=new GControlPosition(G_ANCHOR_BOTTOM_LEFT,new GSize(0,0)); //添加自定义的控件 map.addControl(zcontrol,bottomLeft); //初始化GClientGeocoder对象 GoogleGeocoder=new GClientGeocoder(); var icon=new GIcon(); html='
这是什么
'; icon.image="http://labs.google.com/ridefinder/images/mm_20_red.png"; icon.iconSize=new GSize(21,29); var marker=createMarker(point,icon,html); var point=marker.getPoint(112.429714,39.934522); map.addOverlay(marker); geocoder=new GClientGeocoder(); var marker=new GMarker(center,{draggable:true}); map.addOverlay(marker); document.getElementById("lat").innerHTML=center.lat().toFixed(5); document.getElementById("lng").innerHTML=center.lng().toFixed(5); GEvent.addListener(marker,"dragend",function(){ var point=marker.getPoint(); map.panTo(point); document.getElementById("lat").innerHTML=point.lat().toFixed(5); document.getElementById("lng").innerHTML=point.lng().toFixed(5); }); GEvent.addListener(map,"moveend",function(){ map.clearOverlays(); var center=map.getCenter(); var marker=new GMarker(center,{draggable:true}); map.addOverlay(marker); document.getElementById("lat").innerHTML=center.lat().toFixed(5); document.getElementById("lng").innerHTML=center.lng().toFixed(5); GEvent.addListener(marker,"dragend",function(){ var point=marker.getPoint(); map.panTo(point); document.getElementById("lat").innerHTML=point.lat().toFixed(5); document.getElementById("lng").innerHTML=point.lng().toFixed(5); }); }); } else alert("error"); } //创建右键菜单,参数为地图对象,即为该地图添加右键菜单功能 function createContextMenu(map){ //右键菜单其实是一个DIV contextmenu=document.createElement("div"); //初始创建时右键菜单不可见 contextmenu.style.visibility="hidden"; //设置右键菜单的背景色及宽度 contextmenu.style.background="#ffffff"; contextmenu.style.border="1px solid #8888FF"; //获取地图对象的容器,并将菜单的DIV添加上去,但此时的菜单中不可见的 map.getContainer().appendChild(contextmenu); //为地图的右键添加事件侦听。当右键点击地图时,在地图右键点击的地方把菜单显示出来 GEvent.addListener(map,"singlerightclick",function(pixel,title){ //获取右键点击时的坐标位置(指像素) clickedPixel=pixel; var x=pixel.x; var y=pixel.y; //如果右键点击达到了屏幕的边界,调整菜单的位置 if(x>map.getSize().width-120) { x=map.getSize().width-120 } if(y>map.getSize().height-100) { y=map.getSize().height-100 } //实例化一个地图的位置控件 var pos=new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize(x,y)); //把菜单DIV作为对象传递给地图位置控件 pos.apply(contextmenu); //把菜单设置为可见 contextmenu.style.visibility="visible"; }); //为地图添加click事件侦听器,当单击地图时,把菜单隐藏 GEvent.addListener(map,"click",function(){ contextmenu.style.visiblity="hidden"; }); } function createMarker(point,baseIcon,html){ var icon=new GIcon(baseIcon); var marker=new GMarker(point,icon); GEvent.addListener(marker,"click",function(){ marker.openInfoWindowHtml(html); }); return marker; } //初始化地图 window.onload=load; //卸载地图 window.onunload=GUnload; 厦门旅游服务系统
查询地址: onclick="showLocation();"/>

提示:单击您的当前位置和每个便民设施标注点通过测量距离选择您要的路线

用鼠标拖拉地图中心的红色气球标记就可以显示所在位置的经纬度坐标数据

纬度:

经度:

# Sortable Sortable is a minimalist JavaScript library for reorderable drag-and-drop lists. Demo: http://rubaxa.github.io/Sortable/ ## Features * Supports touch devices and [modern](http://caniuse.com/#search=drag) browsers (including IE9) * Can drag from one list to another or within the same list * CSS animation when moving items * Supports drag handles *and selectable text* (better than voidberg's html5sortable) * Smart auto-scrolling * Built using native HTML5 drag and drop API * Supports * [Meteor](https://github.com/SortableJS/meteor-sortablejs) * AngularJS * [2.0+](https://github.com/SortableJS/angular-sortablejs) * [1.*](https://github.com/SortableJS/angular-legacy-sortablejs) * React * [ES2015+](https://github.com/SortableJS/react-sortablejs) * [Mixin](https://github.com/SortableJS/react-mixin-sortablejs) * [Knockout](https://github.com/SortableJS/knockout-sortablejs) * [Polymer](https://github.com/SortableJS/polymer-sortablejs) * [Vue](https://github.com/SortableJS/Vue.Draggable) * Supports any CSS library, e.g. [Bootstrap](#bs) * Simple API * [CDN](#cdn) * No jQuery (but there is [support](#jq))
### Articles * [Sortable v1.0 — New capabilities](https://github.com/RubaXa/Sortable/wiki/Sortable-v1.0-—-New-capabilities/) (December 22, 2014) * [Sorting with the help of HTML5 Drag'n'Drop API](https://github.com/RubaXa/Sortable/wiki/Sorting-with-the-help-of-HTML5-Drag'n'Drop-API/) (December 23, 2013)
### Install Via npm ```bash $ npm install sortablejs --save ``` Via bower: ```bash $ bower install --save sortablejs ```
### Usage ```html
  • item 1
  • item 2
  • item 3
``` ```js var el = document.getElementById('items'); var sortable = Sortable.create(el); ``` You can use any element for the list and its elements, not just `ul`/`li`. Here is an [example with `div`s](http://jsbin.com/qumuwe/edit?html,js,output). --- ### Options ```js var sortable = new Sortable(el, { group: "name", // or { name: "...", pull: [true, false, clone], put: [true, false, array] } sort: true, // sorting inside list delay: 0, // time in milliseconds to define when the sorting should start touchStartThreshold: 0, // px, how many pixels the point should move before cancelling a delayed drag event disabled: false, // Disables the sortable if set to true. store: null, // @see Store animation: 150, // ms, animation speed moving items when sorting, `0` — without animation handle: ".my-handle", // Drag handle selector within list items filter: ".ignore-elements", // Selectors that do not lead to dragging (String or Function) preventOnFilter: true, // Call `event.preventDefault()` when triggered `filter` draggable: ".item", // Specifies which items inside the element should be draggable ghostClass: "sortable-ghost", // Class name for the drop placeholder chosenClass: "sortable-chosen", // Class name for the chosen item dragClass: "sortable-drag", // Class name for the dragging item dataIdAttr: 'data-id', forceFallback: false, // ignore the HTML5 DnD behaviour and force the fallback to kick in fallbackClass: "sortable-fallback", // Class name for the cloned DOM Element when using forceFallback fallbackOnBody: false, // Appends the cloned DOM Element into the Document's Body fallbackTolerance: 0, // Specify in pixels how far the mouse should move before it's considered as a drag. scroll: true, // or HTMLElement scrollFn: function(offsetX, offsetY, originalEvent, touchEvt, hoverTargetEl) { ... }, // if you have custom scrollbar scrollFn may be used for autoscrolling scrollSensitivity: 30, // px, how near the mouse must be to an edge to start scrolling. scrollSpeed: 10, // px setData: function (/** DataTransfer */dataTransfer, /** HTMLElement*/dragEl) { dataTransfer.setData('Text', dragEl.textContent); // `dataTransfer` object of HTML5 DragEvent }, // Element is chosen onChoose: function (/**Event*/evt) { evt.oldIndex; // element index within parent }, // Element dragging started onStart: function (/**Event*/evt) { evt.oldIndex; // element index within parent }, // Element dragging ended onEnd: function (/**Event*/evt) { var itemEl = evt.item; // dragged HTMLElement evt.to; // target list evt.from; // previous list evt.oldIndex; // element's old index within old parent evt.newIndex; // element's new index within new parent }, // Element is dropped into the list from another list onAdd: function (/**Event*/evt) { // same properties as onEnd }, // Changed sorting within list onUpdate: function (/**Event*/evt) { // same properties as onEnd }, // Called by any change to the list (add / update / remove) onSort: function (/**Event*/evt) { // same properties as onEnd }, // Element is removed from the list into another list onRemove: function (/**Event*/evt) { // same properties as onEnd }, // Attempt to drag a filtered element onFilter: function (/**Event*/evt) { var itemEl = evt.item; // HTMLElement receiving the `mousedown|tapstart` event. }, // Event when you move an item in the list or between lists onMove: function (/**Event*/evt, /**Event*/originalEvent) { // Example: http://jsbin.com/tuyafe/1/edit?js,output evt.dragged; // dragged HTMLElement evt.draggedRect; // TextRectangle {left, top, right и bottom} evt.related; // HTMLElement on which have guided evt.relatedRect; // TextRectangle originalEvent.clientY; // mouse position // return false; — for cancel }, // Called when creating a clone of element onClone: function (/**Event*/evt) { var origEl = evt.item; var cloneEl = evt.clone; } }); ``` --- #### `group` option To drag elements from one list into another, both lists must have the same `group` value. You can also define whether lists can give away, give and keep a copy (`clone`), and receive elements. * name: `String` — group name * pull: `true|false|'clone'|function` — ability to move from the list. `clone` — copy the item, rather than move. * put: `true|false|["foo", "bar"]|function` — whether elements can be added from other lists, or an array of group names from which elements can be taken. * revertClone: `boolean` — revert cloned element to initial position after moving to a another list. Demo: - http://jsbin.com/naduvo/edit?js,output - http://jsbin.com/rusuvot/edit?js,output — use of complex logic in the `pull` and` put` - http://jsbin.com/magogub/edit?js,output — use `revertClone: true` --- #### `sort` option Sorting inside list. Demo: http://jsbin.com/videzob/edit?html,js,output --- #### `delay` option Time in milliseconds to define when the sorting should start. Demo: http://jsbin.com/xizeh/edit?html,js,output --- #### `touchStartThreshold` option This option is similar to `fallbackTolerance` option. When the `delay` option is set, some phones with very sensitive touch displays like the Samsung Galaxy S8 will fire unwanted touchmove events even when your finger is not moving, resulting in the sort not triggering. This option sets the minimum pointer movement that must occur before the delayed sorting is cancelled. Values between 3 to 5 are good. --- #### `disabled` options Disables the sortable if set to `true`. Demo: http://jsbin.com/xiloqu/edit?html,js,output ```js var sortable = Sortable.create(list); document.getElementById("switcher").onclick = function () { var state = sortable.option("disabled"); // get sortable.option("disabled", !state); // set }; ``` --- #### `handle` option To make list items draggable, Sortable disables text selection by the user. That's not always desirable. To allow text selection, define a drag handler, which is an area of every list element that allows it to be dragged around. Demo: http://jsbin.com/newize/edit?html,js,output ```js Sortable.create(el, { handle: ".my-handle" }); ``` ```html
  • :: list item text one
  • :: list item text two
``` ```css .my-handle { cursor: move; cursor: -webkit-grabbing; } ``` --- #### `filter` option ```js Sortable.create(list, { filter: ".js-remove, .js-edit", onFilter: function (evt) { var item = evt.item, ctrl = evt.target; if (Sortable.utils.is(ctrl, ".js-remove")) { // Click on remove button item[removed].removeChild(item); // remove sortable item } else if (Sortable.utils.is(ctrl, ".js-edit")) { // Click on edit link // ... } } }) ``` --- #### `ghostClass` option Class name for the drop placeholder (default `sortable-ghost`). Demo: http://jsbin.com/hunifu/4/edit?css,js,output ```css .ghost { opacity: 0.4; } ``` ```js Sortable.create(list, { ghostClass: "ghost" }); ``` --- #### `chosenClass` option Class name for the chosen item (default `sortable-chosen`). Demo: http://jsbin.com/hunifu/3/edit?html,css,js,output ```css .chosen { color: #fff; background-color: #c00; } ``` ```js Sortable.create(list, { delay: 500, chosenClass: "chosen" }); ``` --- #### `forceFallback` option If set to `true`, the Fallback for non HTML5 Browser will be used, even if we are using an HTML5 Browser. This gives us the possibility to test the behaviour for older Browsers even in newer Browser, or make the Drag 'n Drop feel more consistent between Desktop , Mobile and old Browsers. On top of that, the Fallback always generates a copy of that DOM Element and appends the class `fallbackClass` defined in the options. This behaviour controls the look of this 'dragged' Element. Demo: http://jsbin.com/yacuqib/edit?html,css,js,output --- #### `fallbackTolerance` option Emulates the native drag threshold. Specify in pixels how far the mouse should move before it's considered as a drag. Useful if the items are also clickable like in a list of links. When the user clicks inside a sortable element, it's not uncommon for your hand to move a little between the time you press and the time you release. Dragging only starts if you move the pointer past a certain tolerance, so that you don't accidentally start dragging every time you click. 3 to 5 are probably good values. --- #### `scroll` option If set to `true`, the page (or sortable-area) scrolls when coming to an edge. Demo: - `window`: http://jsbin.com/tutuzeh/edit?html,js,output - `overflow: hidden`: http://jsbin.com/kolisu/edit?html,js,output --- #### `scrollFn` option Defines function that will be used for autoscrolling. el.scrollTop/el.scrollLeft is used by default. Useful when you have custom scrollbar with dedicated scroll function. --- #### `scrollSensitivity` option Defines how near the mouse must be to an edge to start scrolling. --- #### `scrollSpeed` option The speed at which the window should scroll once the mouse pointer gets within the `scrollSensitivity` distance. --- ### Event object ([demo](http://jsbin.com/xedusu/edit?js,output)) - to:`HTMLElement` — list, in which moved element. - from:`HTMLElement` — previous list - item:`HTMLElement` — dragged element - clone:`HTMLElement` - oldIndex:`Number|undefined` — old index within parent - newIndex:`Number|undefined` — new index within parent #### `move` event object - to:`HTMLElement` - from:`HTMLElement` - dragged:`HTMLElement` - draggedRect:` TextRectangle` - related:`HTMLElement` — element on which have guided - relatedRect:` TextRectangle` --- ### Method ##### option(name:`String`[, value:`*`]):`*` Get or set the option. ##### closest(el:`String`[, selector:`HTMLElement`]):`HTMLElement|null` For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree. ##### toArray():`String[]` Serializes the sortable's item `data-id`'s (`dataIdAttr` option) into an array of string. ##### sort(order:`String[]`) Sorts the elements according to the array. ```js var order = sortable.toArray(); sortable.sort(order.reverse()); // apply ``` ##### save() Save the current sorting (see [store](#store)) ##### destroy() Removes the sortable functionality completely. ---
### Store Saving and restoring of the sort. ```html
  • order
  • save
  • restore
``` ```js Sortable.create(el, { group: "localStorage-example", store: { /** * Get the order of elements. Called once during initialization. * @param {Sortable} sortable * @returns {Array} */ get: function (sortable) { var order = localStorage.getItem(sortable.options.group.name); return order ? order.split('|') : []; }, /** * Save the order of elements. Called onEnd (when the item is dropped). * @param {Sortable} sortable */ set: function (sortable) { var order = sortable.toArray(); localStorage.setItem(sortable.options.group.name, order.join('|')); } } }) ``` --- ### Bootstrap Demo: http://jsbin.com/qumuwe/edit?html,js,output ```html <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"/> <!-- Latest Sortable --> [removed][removed] <!-- Simple List -->
  • This is Sortable
  • It works with Bootstrap...
  • ...out of the box.
  • It has support for touch devices.
  • Just drag some elements around.
[removed] // Simple list Sortable.create(simpleList, { /* options */ }); [removed] ``` --- ### Static methods & properties ##### Sortable.create(el:`HTMLElement`[, options:`Object`]):`Sortable` Create new instance. --- ##### Sortable.active:`Sortable` Link to the active instance. --- ##### Sortable.utils * on(el`:HTMLElement`, event`:String`, fn`:Function`) — attach an event handler function * off(el`:HTMLElement`, event`:String`, fn`:Function`) — remove an event handler * css(el`:HTMLElement`)`:Object` — get the values of all the CSS properties * css(el`:HTMLElement`, prop`:String`)`:Mixed` — get the value of style properties * css(el`:HTMLElement`, prop`:String`, value`:String`) — set one CSS properties * css(el`:HTMLElement`, props`:Object`) — set more CSS properties * find(ctx`:HTMLElement`, tagName`:String`[, iterator`:Function`])`:Array` — get elements by tag name * bind(ctx`:Mixed`, fn`:Function`)`:Function` — Takes a function and returns a new one that will always have a particular context * is(el`:HTMLElement`, selector`:String`)`:Boolean` — check the current matched set of elements against a selector * closest(el`:HTMLElement`, selector`:String`[, ctx`:HTMLElement`])`:HTMLElement|Null` — for each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree * clone(el`:HTMLElement`)`:HTMLElement` — create a deep copy of the set of matched elements * toggleClass(el`:HTMLElement`, name`:String`, state`:Boolean`) — add or remove one classes from each element --- ### CDN ```html <!-- jsDelivr :: Sortable (https://www.jsdelivr.com/package/npm/sortablejs) --> [removed][removed] <!-- jsDelivr :: Sortable :: Latest (https://www.jsdelivr.com/package/npm/sortablejs) --> [removed][removed] ``` --- ### jQuery compatibility To assemble plugin for jQuery, perform the following steps: ```bash cd Sortable npm install grunt jquery ``` Now you can use `jquery.fn.sortable.js`:
(or `jquery.fn.sortable.min.js` if you run `grunt jquery:min`) ```js $("#list").sortable({ /* options */ }); // init $("#list").sortable("widget"); // get Sortable instance $("#list").sortable("destroy"); // destroy Sortable instance $("#list").sortable("{method-name}"); // call an instance method $("#list").sortable("{method-name}", "foo", "bar"); // call an instance method with parameters ``` And `grunt jquery:mySortableFunc` → `jquery.fn.mySortableFunc.js` --- ### Contributing (Issue/PR) Please, [read this](CONTRIBUTING.md). --- ## MIT LICENSE Copyright 2013-2017 Lebedev Konstantin http://rubaxa.github.io/Sortable/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
JavaScript:定义行为和动作 (基于对象和事件驱动的客户端脚本语言;也是一种广泛应用于客户端Web开发的脚本语言) 基于对象:网页中的一切元素都是假象!不需要new,即可直接使用 事件驱动:JavaScript的执行都是由事件引发 解释执行:先读到的先执行,后读到的会替代先读的 可以使用任何文本编辑工具编写 JavaScript 代码,然后由浏览器解释执行。 JavaScript常用于实现如下功能: |--控制文档的外观和内容; |--对浏览器的控制; |--与 HTML 表单的交互; |--与用户的交互; |--执行计算等。 1.单击事件:定义在按钮的开始标签中 语法:onclick="js语句" 弹出警告:alert(‘字符串’) 强调:1.js区分大小写 2.字符串单双引号不区分 [removed]标签:页面中专门集中编写JavaScript的区域 js的方法定义:function方法名([参数列表]){ 方法体 [return 返回值] } 注意:js中的方法可以直接写在代码中,不需要“类”包裹 使用方法:方法名()-->方法调用-->立即执行 2.js文件:网页外专门保存js脚本的文件--推荐 强调:HTML、CSS、JS都要使用UTF-8编码保存(window系统) 使用js文件引入网页:[removed][removed] 强调:一旦定义src属性则其中的代码失效 解释执行:语句也可以直接写在js文件中,边解释边执行 3.***调试*** |--①.只有在执行时,才会报错 |--②.错误信息,浏览器页面看不到--没效果 | 解决:控制台--(工具-->JavaScript控制台或F12) | 包含错误信息;指向错误位置的超链接 |--③.打桩:在指定变量位置输出变量或对象的内容 console.log(内容);-->出现在控制台-->日志 4.变量:js中的变量不需要提前指定类型,由赋值时动态决定 所有的变量都用var声明 5.数据类型: |--String(字符串类型) |--Number(数字类型) |--Boolean(布尔类型)0、-0、null、""、false/undefined或NaN,则该对象设置为false。其余都可以当true 6.数据类型的隐式转换: |--数字 + 字符串:数字转换为字符串 |--数字 + 布尔值:true转换为1,false转换为0 |--字符串 + 布尔值:布尔值转换为字符串true或false |--布尔值 + 布尔值:布尔值转换为数值1或0 7.数据类型转换函数 :(方法前不需要对象调用的:全局函数) |--toString():转换成字符串。所有数据类型均可转换为 string 类型; |--parseInt():强制转换成整数。如果不能转换,则返回 NaN(not a number); |--parseFloat():强制转换成浮点数。如果不能转换,则返回 NaN(Not a Number) |--typeof():查询数值当前类型。 |--isNaN():判断是否为数字。返回 true:不是数字/false:是数字 | |--isNan(""):对空字符串不验证,直接返回false | |--如果输入的是字符串类型的数字,返回false--不能判断数据类型,只判断内容 | |--如果输入的是boolean,返回false。因为boolean可以和number类型直接做计算 | |--和任何数字计算都得NaN;和任何数字作比较都得false |--注:[removed]();节点输出,即在当前位置输出括号里的内容 *凡是从页面上进入js的都是字符串类型 8.查找元素:抓住根节点,就等于抓住整棵树 网页的根节点:document对象 要找元素,必须利用document对象(当前网页文件) 精确查找某个ID的元素:document.getElementById(id名); 9.null和undefined: |--null:一个特殊的值,表示“无值”--空对象。数据类型为Object |--undefined:表示声明了变量但从未赋值或者对象属性不存在 10.双等号(==)和全等号(===): |--“==”为确定两个运算数是否相等,“==”运算符会进行类型转换。转换后运算数相等就返回true,否则返回false |--“===”不执行类型转换,即,只有在无需类型转换运算数就相等的情况下,才返回true,否则返回false |--附:if(null)、if(defined)、if(NaN)都相当于if(false) -----猜数字游戏----- 失去焦点时,判断猜对猜错 获得焦点:onfocus 失去焦点:onblur 当事件就发生在获得内容的当前元素上: this直接获得当前内容对象。可以代替当前对象完成一切操作,拥有当前对象的所有属性 前端优化:js中最好用三目运算代替if else 11.String: |--查找:x.indexOf(‘关键字’[,开始位置下标]) | 每次只查找第一次出现的位置 |--替换:x.replace(‘关键字’[,‘替换内容’])--不会修改原x字符串 | 每次只替换第一次找到的 |--查找和替换所有:while循环 | |--String 对象的常用方法有: |--x.toLowerCase()、x.toUpperCase():大小写转换方法; |--x.charAt(index):返回指定位置的字符; |--x.charCodeAt(index):返回指定位置字符的Unicode编码; |--x.indexOf(findstr,index)、x.lastIndexOf(findstr,index): 获取指定字符; |--x.substring(start, end): 获取子字符串; |--x.replace(findstr,tostr):替换子字符串; |--x.split(bystr): 拆分子字符串。 |--String与正则表达式 |--str.match(regExp);--查找str中匹配正则表达式的关键字 | 返回:如果没找到,返回null。若找到,返回1个数组,数组的每个元素是每个找到的匹配关键 | |--str.replace(regExp,"替换值")--替换str中所有匹配的关键字 | 强调:replace方法不改变元字符串,只能返回新字符串。必须用变量接收新字符串 | |--js中正则表达式语法:/正则表达式/[属性后缀]--其中属性后缀,g:全局匹配 i:忽略大小写(仅英文有效) 如果不适用模式匹配方式,将执行原文匹配 结果:如果正则表达式写错,也将执行原文匹配 12.Array笔试题:js中数组声明方式: A new Array(7) B new Array(7,‘a’,true) C [7,'a',true]--js中所有[]都表示数组 D []--实例化一个空数组对象 实例化空数组:var arr=[]; 特点:元素个数不限定,元素类型不限制 13.Array 对象的常用方法: |--1.join()方法--用于把数组中的所有元素放入一个字符串   | eparato表示要使用的分隔符。如果省略该参数,则使用逗号作为分隔符 |--2.toString()方法--可把数组转换为字符串,并返回结果 |--3.concat()方法--用于连接两个或多个数组,该方法不会改变现有的数组 |--4.slice()方法--截取元素。根据给定的范围可从已有的数组中返回选定的元素 14.Function:js中一切都是对象,连方法都是1个对象!! 笔试题:js中方法定义集中方式: A:function compare(a,b){return a-b;}---*可以任意地方声明方法* B:var compare=function(a,b){return a-b;} ---| --其实方法名也是方法对象的变量名;等号右边其实就是1个匿名方法对象 |--*必须在传递之前声明方法对象* C:var compare=new Function('a','b','return a-b') ---| --其实js底层就是new Function;构造函数中的参数都是字符串 结论:所有的方法都是function类型的。 15.JavaScript中的所有事物都是对象,分为三类: |--简单对象:String、Number、Boolean |--组合对象:Array、Math、Date |--复杂对象:Function、Regex、Object等 16.Array排序: |--升序:function compare(a,b){return a-b;} |--降序:function compare(a,b){return b-a;} 比较器用法:arr.sort(比较器方法名); 遍历:for(var i=0;idocument:窗口中显示的 HTML 文档对象 |--history:本次浏览过窗口的历史记录 | --前进:history.go(1);后退:history.go(-1);刷新:history.go(0); |--location:窗口文件地址对象(地址栏) |--event:事件对象 |--screen:屏幕对象 |--name:窗口名称 |--opener:打开当前窗口的 window 对象 |--navigator:有关浏览器的信息 |--cookieEnable:判断当前浏览器是否启用cookie |--userAgent:获得浏览器的名称和版本号 window 对象的常用方法有: |--alert();--警告框--只能点确认 |--confirm();确认框--可以选择确认或取消 |--prompt();对话框--用于显示可提示用户进行输入 |--window.open('url'[,'name']):打开1个选项卡 |--window.close():关闭当前选项卡 24.定时器:凡是网页中自动动态的效果都是用计时器实现的 |--周期性定时器:每隔一个时间段自动执行一次,循环执行 | |--setInterval() 启动-->用于启动一个周期性定时器 | 语法:timer=setInterval(方法名,间隔毫秒数) |--clearInterval() 停止-->用于停止一个周期性定时器 语法:timer=clearInterval(timer) |--1次性定时器:先等待一段时间,再自动执行一次,自动结束 |--setTimeout() 启动-->用于启动一个一次性定时器 |--clearTimeout() 停止-->用于停止一个一次性定时器 附: |--表单控件,读写内容:.value |--普通html元素,要想读写开始标签和结束标签之间的内容:[removed] 计时器:1.做什么事:方法; 2.一个变量:存计时器的线程号 3.何时启动计时器:事件 25.document:1.代表当前网页文档; 2.所有网页元素的根元素; 3.查找和操作元素,都要依靠document; 节点:nodeName--标签名或属性名 DOM操作样式:对象.className属性 等效于<标签class=""> DOM树:当前节点向上:txtObj[removed]() 向下:txtObj.getElementByTagName() 26.表单中查找控件对象:document.getElementsByName('name属性') DOM增加新节点:两种方法 创建新节点:document.creatElement('标签名') --此时新元素对象仅在内存中游离,页面看不见! 添加新节点:找到父元素! |--appendChild() 方法只能将新节点作为某节点的最后一个子节点 | --此时新元素追加到了父元素的末尾 |--parentNode.insertBefore()(新元素对象,A)--其中A为新元素要插入位置的后一位元素 DOM删除元素:node.removeChild(childNode)--node为父节点,childNode为要删除的节点 27.HTML DOM Select和Option对象: select对象:代表HTML表单中的一个下拉列表,每个元素中所有