[JavaScript]JS屏蔽浏览器右键菜单/粘贴/复制/剪切/选中 [转载]
前两天在解决一个项目缺陷时,突发感兴趣,了解一下~
1 JS事件
document.oncontextmenu // 右键菜单
document.onpaste //粘贴
document.oncopy //复制
document.oncut //剪切
document.onselectstart //选中开始时
+ 触发时间为目标对象被开始选中时(即选中动作刚开始,尚未实质性被选中)
+ 基本上都能支持,但不被 input 和 textarea 标签支持
+ 注意:如果想在火狐中禁用的话可以使用样式控制 div { -moz-user-select: none; }
document.onselect //选中已发生时
+ 文本框中的文本被选中时发生
+ 被 input 和 textarea 标签支持
补充: select相关API
- 取消诸多网页中选中事件对版权的保护措施,可尝试如下几种方法:
- 控制台输入:
$=0
- 首先, Ctrl + P (打印当前网页) ; 然后在打印的预览界面中选中目标文本,并鼠标右键选择"复制(Ctrl+C)"拷贝之~
- 利用 document.getSelection()/selection的API来获取选中的文本
document.getSelection().focusNode.wholeText //选中的当前元素的文本
document.getSelection().focusNode.parentElement.parentNode.innerText
document.getSelection().focusNode.parentNode.previousElementSibling.innerText
document.getSelection().focusNode.parentElement.nextElementSibling.innerText
- document.selection / document.getSelection
IE:
支持 document.selection
不支持 document.getSelection
FireFox / Safari:window.getSelection
不支持 document.selection
支持 document.getSelection
Chrome (86.0.4240.111):
不支持 document.selection
支持 document.getSelection
document.selection / document.getSelection 都不是标准语法。
--------------------------------------------------------------------------------
【 document.getSelection() 函数返回的对象 】
获取选中的文本: document.getSelection().[anchorNode/baseNode/extentNode/focusNode/].[wholeText/textContent/data/nodeValue]
【selection 对象】
代表了当前激活选中区,即高亮文本块,和/或文档中用户可执行某些操作的其它元素。
selection 对象的典型用途是作为用户的输入,以便识别正在对文档的哪一部分正在处理,或者作为某一操作的结果输出给用户。
用户和脚本都可以创建选中区。用户创建选中区的办法是拖曳文档的一部分。脚本创建选中区的办法是在文本区域或类似对象上调用 select 方法。要获取当前选中区,请对 document 对象应用 selection 关键字。要对选中区执行操作,请先用 createRange 方法从选中区创建一个文本区域对象。
一个文档同一时间只能有一个选中区。选中区的类型决定了其中为空或者包含文本和/或元素块。尽管空的选中区不包含任何内容,你仍然可以用它作为文档中的位置标志。
[选中目标DOM]
// 获取selection对象
var selection = window.getSelection();
// 清空selection对象
selection.removeAllRanges();
// 创建1个Range实例
var ele = document.getElementById('blogTitle')
var range = new Range();
range.selectNodeContents(ele);
// selection对象设置range实例
selection.addRange(range);
2 JS源码
//屏蔽右键菜单 (<input> / <textarea> / type=text 除外)
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;
}
}
//屏蔽粘贴 (<input> / <textarea> / type=text 除外)
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; // false 表示 禁用 粘贴功能
}
return true;
}catch (e){
return false;
}
}
//屏蔽复制 (<input> / <textarea> / type=text 除外)
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;
}
}
//屏蔽剪切 (<input> / <textarea> / type=text 除外)
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;
}
}
//屏蔽(文本)选中 (<input> / <textarea> / type=text 除外)
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;
}
}
3 附件: 完整源码
展开查看
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js屏蔽浏览器右键菜单,粘贴,复制,剪切,选中</title>
<meta name="description" content="js代码制作屏蔽浏览器右键菜单,屏蔽浏览器粘贴,屏蔽浏览器复制,屏蔽浏览器剪切,屏蔽浏览器选中,不会屏蔽搜索引擎蜘蛛抓取页面,不影响seo优化。可以设置网页内容代码不被用户随意下载等。" />
</head> <body> <input type="input" /> <script type="text/javascript"> //屏蔽右键菜单 (<input> / <textarea> / type=text 除外)
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;
}
} //屏蔽粘贴 (<input> / <textarea> / type=text 除外)
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; // false 表示 禁用 粘贴功能
}
return true;
}catch (e){
return false;
}
} //屏蔽复制 (<input> / <textarea> / type=text 除外)
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;
}
} //屏蔽剪切 (<input> / <textarea> / type=text 除外) 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;
}
} //屏蔽(文本)选中 (<input> / <textarea> / type=text 除外)
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;
}
}
</script> <div style="width:600px;margin:40px auto;text-align:center;border:solid 1px #FFCA52;background:#FFFDD2;height:28px;line-height:28px;font-size:14px;padding:5px 10px;color:#ff0000;font-weight:800;">单页禁用右键菜单、粘贴、复制、剪切、选中操作</div>
test
</body>
</html>
X 参考与推荐文献
- js屏蔽浏览器右键菜单,粘贴,复制,剪切,选中(转) - 博客园
- 关于markdown 折叠代码块 / Markdown显示HTML标签以及转义字符 - 博客园
- onselect 与 onselectstart - 简书
[JavaScript]JS屏蔽浏览器右键菜单/粘贴/复制/剪切/选中 [转载]的更多相关文章
- js屏蔽浏览器右键菜单,粘贴,复制,剪切,选中(转)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- js 屏蔽浏览器右键菜单
<script type="text/javascript"> function doNothing(){ window.event.returnValue=false ...
- js屏蔽浏览器右键菜单
<script type="text/javascript"> function doNothing(){ window.event.returnValue=false ...
- 利用js代码屏蔽f12,右键,粘贴,复制,剪切,选中,操作!!秀!秀!秀!
koala 专注于个人技术分享 屏蔽f12审查 <script> document.onkeydown = function () { if (window.event && ...
- JavaScript 中禁止用户右键菜单,复制,选取,Ctrl,Alt,Shift. 获取宽高,清除浮动
//禁用右键菜单 document.oncontextmenu = function(){ event.returnValue = false; } //禁用选取内容 document.onselec ...
- JS 禁止浏览器右键菜单和刷新
<script language="javascript"> //禁止按键F5 document.onkeydown = function(e){ e = window ...
- javascript屏蔽浏览器右键功能按钮
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 4.js屏蔽浏览器鼠标右键菜单
document.oncontextmenu = function(){return false;} 参考链接:js 屏蔽浏览器事件汇总
- javascript自定义浏览器右键菜单
javascript自定义浏览器右键菜单 在书上看到document对象还有一个contextmenu事件,但是不知为什么w3school中找不到这个耶... 利用这个特性写了个浏览器的右键菜单, ...
- js屏蔽浏览器(IE和FireFox)的刷新和右键等功能
//一.js屏蔽浏览器(IE和FireFox)的刷新功能 document.onkeydown=function() { if ((window.event.keyCode==116)|| //屏蔽 ...
随机推荐
- python3GUI--实用!B站视频下载工具(附源码)
目录 一.准备工作 二.预览 1.启动 2.解析 3.下载中 4.下载完成 5.结果 三.设计流程 1.bilibili_video_spider 2.视频json的查找 四.源代码 1.Bilibi ...
- nRF51822蓝牙学习 进程记录 3:蓝牙协议学习--简单使用
三天打鱼两天晒网,又学起了蓝牙,不过还好的是终于开始学习蓝牙协议部分了. 但是,一看起来增加了蓝牙协议的例程,真是没头绪啊.本身的教程资料解说太差了,看青风的蓝牙原理详解也是一头雾水. 经过不断地看各 ...
- MMDetection中模型的Checkpoints下载
mmdetection中的模型checkpoints是需要自己手动下载的,下载步骤如下: 打开mmdetection, 进入configs目录,可以看到这里面有很多以目标检测模型命名的文件夹,选择你想 ...
- 【Unity】阅读LuaFramework_UGUI的一种方法
写在前面 我第一次接触到LuaFramework_UGUI是在一个工作项目中,当时也是第一次知道toLua.但我刚开始了解LuaFramework_UGUI时十分混乱,甚至将LuaFramework_ ...
- rust-must-know-crates-5ad8 100DayOfRust
https://dev.to/cad97/rust-must-know-crates-5ad8 https://dev.to/search?q=100DayOfRust https://fastert ...
- 小米盒子TV变装魔法
最近从一位台湾的朋友那里白嫖了一个 v2 节点, 恰好家里有一台家用的 小米盒子, 就寻思着能不能折腾一下, 共享上网 先将小米盒子开启adb调试, 参照这里: https://www.jb51.ne ...
- 微信小程序页面间通的5种方式
PageModel(页面模型)对小程序而言是很重要的一个概念,从app.json中也可以看到,小程序就是由一个个页面组成的. 如上图,这是一个常见结构的小程序:首页是一个双Tab框架PageA和Pag ...
- C/C++ 数据结构使用数组实现队列的基本操作
//使用数组实现队列 #include <iostream> #include <Windows.h> using namespace std; #define MAXSIZE ...
- Blob、FormData
Blob 在我的理解中这个就是一个二进制的存储类型,就像一张图片就是一组二进制,很多文件都是一组二进制.这个就是数据库用来存储二进制类型. FormData 为什么使用 FormData 来进行数据的 ...
- jenkins脚本
1.统计代码 pipeline { agent any parameters { choice( description: '你需要选择当前哪个分支进行统计 ?', name: 'branchNow' ...