亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

html網頁表單中禁用復制、右鍵、粘貼、剪切等方法

標簽:
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;  
    }  
}  

//屏蔽粘贴  
document.onpaste = 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;  
    }  
}  

//屏蔽复制  
document.oncopy = 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;  
    }  
}  

//屏蔽剪切  
document.oncut = 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;  
    }  
}  

//屏蔽选中  
document.onselectstart = 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;  
    }  
}

网页退出提示的方法:

window.onbeforeunload = function(event){  
            event = event || window.event;  
            event.returnValue = ' ';  
    }  

移动端中,屏蔽类似iphone的默认滑动事件用一下方法:

//禁用浏览器的默认滑动事件  
    var preventBehavior = function(e) {  
        e.preventDefault();  
    };  
    // Enable fixed positioning  
    document.addEventListener("touchmove", preventBehavior, false);  

直接在CSS 文件中添加下面的代码,就可以实现了在手机端禁止粘贴复制的功能:

1.*{   
2.    -webkit-touch-callout:none;  /*系统默认菜单被禁用*/   
3.    -webkit-user-select:none; /*webkit浏览器*/   
4.    -khtml-user-select:none; /*早期浏览器*/   
5.    -moz-user-select:none;/*火狐*/   
6.    -ms-user-select:none; /*IE10*/   
7.    user-select:none;   
8.}  

在添加完这段代码后,在IOS 上会有问题的,这个时候你会发现input 框无法正在输入了内容了;造成这个原因就是 -webkit-user-select:none; 这个属性造成的。

解决这个方法 就是 在css 文件中同时设置一下input 的属性,如下所示:

1.input {      
2.     -webkit-user-select:auto; /*webkit浏览器*/     
3.}  
點擊查看更多內容
1人點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
Web前端工程師
手記
粉絲
82
獲贊與收藏
1103

關注作者,訂閱最新文章

閱讀免費教程

感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消