javascript disableuserselect 防止用户选择...
时间:2010-08-06 来源:baoeni
有时候当我们要拽动某个控件的时候,浏览器会以为我们要选择网页内容,然后出现一大片难看的选择区域,为了避免这种情况,各大javascript库都都提供类似的函数: 基本有一下几种方法: CSS: 对于firefox:style.MozUserSelect = "none" 对于webkit:style.WebkitUserSelect = "none" IE: 对于IE: element.unselectable = "on" 防止事件: disable mousedown, selectstart, dragstart ================================== bb.html.disableUserSelect = function (oElm){ //-------------------------------- oElm.style.MozUserSelect = "none"; //Mozilla oElm.style.WebkitUserSelect = "none"; //Safari bb.AddEventListener(oElm, 'mousedown', bb.html.handlePreventDefault, false); //Mozilla, Opera, Safari bb.AddEventListener(oElm, 'selectstart', bb.html.handlePreventDefault, false); //IE bb.AddEventListener(oElm, 'dragstart', bb.html.handlePreventDefault, false); //IE }; But Dojo do: dojo.html.disableSelection = function(/*DomNode*/element){ // summary: disable selection on a node element = dojo.byId(element)||dojo.body(); var h = dojo.render.html; if(h.mozilla){ element.style.MozUserSelect = "none"; }else if(h.safari){ element.style.KhtmlUserSelect = "none"; }else if(h.ie){ element.unselectable = "on"; }else{ return false; } return true; }
相关阅读 更多 +