JavaScript 这个代码,返回值 return false;是为了什么?
本文介绍了JavaScript 这个代码,返回值 return false;是为了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
初学者,这个代码,是之前别人告诉我的,我一句句学习了,
别的理解的差不多了,就是不懂为什么要有一个 返回值 false
他是干什么的?
if (window.Event)
document.captureEvents(Event.MOUSEUP);
function nocontextmenu()
{
event.cancelBubble = true
event.returnValue = false;
return false;//★★★★★★★★★★★★★★
}
function norightclick(e)
{
if (window.Event) {
if (e.which == 2 || e.which == 3)
return false;
} else if (event.button == 2 || event.button == 3){
event.cancelBubble = true
event.returnValue = false;
return false; //★★★★★★★★★★★★★★
}
}
document.oncontextmenu = nocontextmenu; // for IE5+
document.onmousedown = norightclick; //for all others
解决方案
function nocontextmenu()
{
event.cancelBubble = true
event.returnValue = false;
return false;//★★★★★★★★★★★★★★
}
返回false是为了让document.oncontextmenu设置为false来禁用右键菜单
function norightclick(e)
{
if (window.Event) {
if (e.which == 2 || e.which == 3)
return false;
} else if (event.button == 2 || event.button == 3){
event.cancelBubble = true
event.returnValue = false;
return false; //★★★★★★★★★★★★★★
}
}
返回false是设置document.onmousedown = false来然让鼠标按键失效
其实nocontextmenu()
和norightclick(e)
这2个方法已经达到效果了,在设置这个属性无非就是效果更佳!
这篇关于JavaScript 这个代码,返回值 return false;是为了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文