当前位置: 代码迷 >> Web前端 >> 页面障蔽鼠标左右键备忘
  详细解决方案

页面障蔽鼠标左右键备忘

热度:123   发布时间:2012-08-26 16:48:05.0
页面屏蔽鼠标左右键备忘

2012-3-30?

由于在不同浏览器下对于ctrl+p的效果不同,有的还是能打印,并且使用浏览器上的打印也可以,再次更改

?

在页面头上加入以下代码,即使能打印也只是打印空白页面。

?

<style unselectable="on" style="-webkit-user-select: none; ">
			@media print {
			* { display: none }
			}
</style>
?

--------------------------------------------------------

2012-3-16 使用jquery来屏蔽鼠标左右键以及ctrl+a?ctrl+c?ctrl+v?ctrl+p

?

?

 (function($){
 $.fn.ctrl = function(key, callback) {
    if(typeof key != 'object') key = [key];
    callback = callback || function(){ return false; }
    return $(this).keydown(function(e) {
        var ret = true;
        $.each(key,function(i,k){
            if(e.keyCode == k.toUpperCase().charCodeAt(0) && e.ctrlKey) {
                ret = callback(e);
            }
        });
        return ret;
    });
};
$.fn.disableSelection = function() {
    $(window).ctrl(['a','s','c','p']);
    return this.each(function() {           
        $(this).attr('unselectable', 'on')
               .css({'-moz-user-select':'none',
                    '-o-user-select':'none',
                    '-khtml-user-select':'none',
                    '-webkit-user-select':'none',
                    '-ms-user-select':'none',
                    'user-select':'none'})
               .each(function() {
                    $(this).attr('unselectable','on')
                    .bind('selectstart',function(){ return false; })
					.bind("contextmenu",function(){return false;});
               });
    });
};
})(jQuery);

$(function(){$(document).disableSelection();});

?

下午3点更正,经测试,上面代码在ff中不能屏蔽select,经实验后,再加一上句js即可对执行语句进行更改,在ff上也能屏蔽

?

$(function(){$(document.body).disableSelection();});

?

下午5点更正,经测试,上面代码在ie下不能屏蔽ctrl+p,再加一句纯js来单独屏蔽ie下的打印快捷键

?

document.onkeydown=function(){if(event.ctrlKey&&event.keyCode==80){event.keyCode=0;event.returnValue=false;}}
?

---------------------------------------------------------

2012-3-14 使用js来屏蔽鼠标左右键

?

$(function(){
	var target=document.body;
	if (typeof target.onselectstart!="undefined") //IE route
		target.onselectstart=function(){return false}
	else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
		target.style.MozUserSelect="none"
	else //All other route (ie: Opera)
		target.onmousedown=function(){return false}
	target.style.cursor = "default";
 $(document).bind("contextmenu",function(e){return false;});
});
  相关解决方案