zoukankan      html  css  js  c++  java
  • 右键禁用、防止文字选中 、返回选中的文本

    event事件参考: 

    http://www.mangguo.org/8-jquery-tip-and-trick/

    http://bbs.blueidea.com/forum.php?mod=viewthread&tid=273549

    1  右键菜单的禁用:

    网上有几种方法:

    1.

    <script>
    document.oncontextmenu = new Function("return false");
    </script>

    2.

    还有更简单的,直接在<body>里加入以下代码也同样有效:
    oncontextmenu="return false"

    3.

    在BODY中加入如下代码
    oncontextmenu="self.event.returnValue=false" onselectstart="return false"
    onkeydown="if(event.keyCode==78&amp;&amp;event.ctrlKey)return false;"

    右键禁用:

    1.

    jQuery(function($){
      $(document).bind("contextmenu",function(){
            return false;
       });
    })

    2.

    <script type="text/javascript">
    //屏蔽文本框以外右键菜单
    document.oncontextmenu = function (event){
    	if(window.event){
    		event = window.event;
    	}
    	try{
    		var the = event.srcElement;
    		if (!((the.tagName == "INPUT" && the.type.toLowerCase() == "text") || the.tagName == "TEXTAREA")){
    			return false;
    		}
    		return true;
    	}catch (e){
    		return false; 
    	} 
    }
    </script>
    
    

    2.禁止鼠标选中文字代码(会用到拖动效果中)

    document.onmousemove=function(){
    //禁止选中片段
    try{
          if(document.selection){//IE ,Opera
              if(document.selection.empty)
                       document.selection.empty();//IE
              else{//Opera
                       document.selection = null;
              }
          }else if(window.getSelection){//FF,Safari
               window.getSelection().removeAllRanges();
          }
    }catch(e){}
    
    }
    

    返回选中的文本:

    <script type="text/javascript">
        $(function () {
                $(document).mouseup(function (e) {
                    var txt = "";
                    if (document.selection) {
                        txt = document.selection.createRange().text;	// IE
                    } else {
                        txt = document.getSelection();
                    }
                    txt = txt.toString();
                    alert(txt)
                })
        })
    </script>

    3.鼠标按键

    jquery 关于鼠标按键:

    在jquery中只有三个值,通过e.which 判断:

    左键:1

    右键:2

    中间键:3

    例如:在mousedown事件中 点击三个鼠标键都会有反应,可以加入判断:

    jQuery(function($){
        $("#aa").mousedown(function(e){
            if(e.which==1){
               //do something;
            } 
       })
    })

    原生js 关于鼠标按键:

    event.button:

    可能的值:
    0 没按键
    1 按左键
    2 按右键
    3 按左右键
    4 按中间键
    5 按左键和中间键
    6 按右键和中间键
    7 按所有的键

    这个属性仅用于onmousedown, onmouseup, 和 onmousemove 事件。对其他事件,不管鼠标状态如何,都返回 0(比如onclick)在firefox中,只会出现三个值,分别是0 、1、 2(左、中、右)

    例:

    <script type="text/javascript">
    function omd(e){
      var k=(e?e:event).button;
      document.getElementById("aaa").innerHTML=k;
    }
    </script>
    <div onmousedown="omd(event)">
    点击我,我告诉你你按的是鼠标哪个键<br />
    同时按下鼠标上的多个键试试
    </div>
    <br />键值:<span id="aaa"></span>
  • 相关阅读:
    算法之路——插入排序篇3:希尔排序
    对enum的探讨
    算法之路——插入排序篇1
    算法之路——快速排序
    算法之路——插入排序篇2
    圆的角度DDA算法初试
    解决omnicppcomplete显示"pattern not found"
    汇编
    直线的dda算法
    equ定义的是符号
  • 原文地址:https://www.cnblogs.com/hdchangchang/p/3965383.html
Copyright © 2011-2022 走看看