在<body>标签中添加以下代码:
oncontextmenu='return false' 禁止右键
ondragstart='return false' 禁止拖动
onselectstart ='return false' 禁止选中
onselect='document.selection.empty()' 禁止选中
oncopy='document.selection.empty()' 禁止复制
onbeforecopy='return false' 禁止复制
onmouseup='document.selection.empty()'
扩展资料
示例代码如下:
<body leftmargin=0 topmargin=0 oncontextmenu='return false' ondragstart='return false' onselectstart ='return false' onselect='document.selection.empty()' oncopy='document.selection.empty()' onbeforecopy='return false' onmouseup='document.selection.empty()'>
需要准备的材料分别有:电脑、浏览器、html编辑器。
1、首先,打开html编辑器,新建html文件,例如:index.html,输入问题基础代码。
2、在index.html中的<body>标签中,输入样式代码:
onmousemove=HideMenu() oncontextmenu="return false" ondragstart="return false" onselectstart ="return false" onselect="document.selection.empty()"
oncopy="document.selection.empty()" onbeforecopy="return false" onmouseup="document.selection.empty()"
3、浏览器运行index.html页面,此时内容无法右键和选中以复制。
在网页开发中,有些时候我们不想让用户去复制或者粘贴该网页的东西,那么下面的几个方法就非常有用了!
//屏蔽右键菜单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)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)