onclick事件中checkForm()和return checkForm()的区别

本文通过一个简单的示例,介绍了HTML中a标签的使用方法及其与onclick事件的关系。测试发现,在a标签的onclick属性中使用return时,checkForm()方法必须返回true才能触发跳转;若不使用return,则无论checkForm()返回什么值都会触发跳转。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先上代码:

<!DOCTYPE html>
<html>
<head>
    <title>test</title>
</head>
<body>
    <div>
        <a href="www.baidu.com" onclick="return checkForm()">点击我</a>
    </div>
</body>
</html>
<script type="text/javascript">
    function checkForm()
    {
        return true;
    }
</script>

测试结果:测试发现,onclick里加return时,checkForm()方法必须返回true才可以触发a标签的跳转。而不加return的时候,无论checkForm()返回false还是true,都会触发a标签的跳转。

06-23
以下源码的用户密码分别是:<!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" xml:lang="zh-CN" lang="zh-CN"> <head> <meta HTTP-EQUIV="Pragma" CONTENT="no-cache"> <meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=GBK"> <title>安邦系统平台入口-登录验证</title> <style> *{margin:0px;padding:0px;} html,body{margin:0px; padding:0px;height:100%;width:100%;border:0px;} body{background:#6c6c6c;height:100%;width:100%;overflow:hidden;} .LoginDIV{width:700px;margin:0px auto;;padding:0px;overflow:hidden;} .FootDIV {width:700px;margin:0px auto;;padding:0px;overflow:hidden;text-align:center;} .TextInput{padding:3px 0px 3px 5px;width:100px;border-left:solid 1px #444444;border-top:solid 1px #444444;border-right:solid 1px #ffffff;;border-bottom:solid 1px #ffffff;font-size:13px;text-align:center;} </style> <script type="text/javascript" src="/js/jquery-1.8.3.min.js"></script> <script language='JavaScript'> function getObjByID(Id){if('object'==typeof(Id))return Id;else if('string'==typeof(Id))return document.getElementById(Id);else return null;} function SetCookie(inc_Name,INC_Value,INC_Hours) {   var TheEXP = new Date();   TheEXP.setTime(TheEXP.getTime() + 60*60*1000*INC_Hours);   document.cookie= inc_Name + "=" + INC_Value + ";expires="+ TheEXP.toGMTString(); } function GetCookie(inc_Name) {   var cookieString = new String(document.cookie);   var cookieHeader = inc_Name + "=";   var beginPosition = cookieString.indexOf(cookieHeader);   if (beginPosition != -1) { cookieString=cookieString.substring(beginPosition + cookieHeader.length); beginPosition=cookieString.indexOf(";") if (beginPosition != -1){cookieString=cookieString.substring(0,beginPosition);} return cookieString; }   else {return "";} } function GetAllCookie() { var CookieUserName=GetCookie('CKUserName'); var CookieUserPass=GetCookie('CKUserPass'); var CookieKeepMyInfo=GetCookie('CKKeepMyInfo'); document.getElementById('UserName').value=CookieUserName; document.getElementById('UserPass').value=CookieUserPass; if (CookieKeepMyInfo.replace(/\s*/,'')=='1') {document.getElementById('KeepMyInfo').value==1;} else {document.getElementById('KeepMyInfo').value==0;} } function CheckForm(inc_form) { var FOBJ=document.getElementById(inc_form); var UserName=FOBJ.UserName.value.replace(/\s*/g,''); var UserPass=FOBJ.UserPass.value.replace(/\s*/g,''); if (UserName==''){alert('请输入帐号');FOBJ.UserName.value='';FOBJ.UserName.focus();return false;} if (UserPass==''){alert('请输入密码');FOBJ.UserPass.value='';FOBJ.UserPass.focus();return false;} if (UserPass==''){alert('请输入密码');FOBJ.UserPass.value='';FOBJ.UserPass.focus();return false;} if (document.getElementById('KeepMyInfo').value==1) { SetCookie("CKUserName",UserName,48); SetCookie("CKUserPass",UserPass,48); SetCookie("CKKeepMyInfo",1,48); } else { SetCookie("CKUserName","",48); SetCookie("CKUserPass","",48); SetCookie("CKKeepMyInfo",0,48); } return true; } function CheckKeydown(inc_event,inc_NextOBJ,inc_FS) { if (inc_event.keyCode==13) { if (inc_FS==0) {document.getElementById(inc_NextOBJ).focus();} else { if(CheckForm(inc_NextOBJ)){document.getElementById(inc_NextOBJ).submit();} } } } function resizeWindow() { var BodyHeight=0; var TopDIVHeight=0; var LoginDIVHeight=0; var IMGLaoYingHeight=0; var IMGLaoYingWidth=0; BodyHeight=$("body").height(); LoginDIVHeight=$("#LoginDIV").height(); IMGLaoYingHeight=$("#IMGLaoYing").height(); TopDIVHeight=BodyHeight-LoginDIVHeight; var IMGMarginTop= (TopDIVHeight-IMGLaoYingHeight)/2.5 + 'px'; $("#TopDIV").height(TopDIVHeight); $("#IMGLaoYing").css("margin-top",IMGMarginTop); } $(document).ready(function(){resizeWindow();}); $(window).resize(function() {resizeWindow();}); </script> </head> <body onload="GetAllCookie();document.getElementById('UserName').focus();"> <div id="TopDIV" style="overflow:hidden;text-align:center;background:#000000;margin:0px auto;width:100%;background:#000000;height:70%"> <img id='IMGLaoYing' src="/images/laoying.png" style="height:55%;max-width:650px;"> </div> <div id="LoginDIV" style="overflow:hidden;text-align:center;background:#000000;margin:0px auto;width:100%;background:#c07814;height:200px"> <div style="overflow:hidden;height:50px;"> </div> <div style="overflow:hidden;margin:0px auto;width:500px;text-align:center;border:solid 0px #000000"> <form method='post' id='LogForm' name='LogForm' action='CheckPWD.php' style='margin:0px;padding:0px;overflow:hidden;'> <div style="overflow:hidden;float:left;margin-left:5px;"></div> <div style="overflow:hidden;float:left;font-size:13px;padding-top:3px;color:#222222;font-weight:bold;font-family:'Microsoft YaHei' ! important;">用  户:</div> <div style="overflow:hidden;float:left;margin-left:5px;"><Input Type="Text" class="TextInput" name="UserName" id="UserName" value="" maxlength="30" onkeydown="CheckKeydown(event,'UserPass',0);"></div> <div style="overflow:hidden;float:left;width:60px;"> </div> <div style="overflow:hidden;float:left;font-size:13px;padding-top:3px;color:#222222;font-weight:bold;font-family:'Microsoft YaHei' ! important;">密  码:</div> <div style="overflow:hidden;float:left;margin-left:5px;"><Input Type="password" class="TextInput" name="UserPass" id="UserPass" value="" maxlength="30" onkeydown="CheckKeydown(event,'LogForm',1);"></div> <div style="overflow:hidden;float:left;width:55px;"> </div> <div style="overflow:hidden;float:left;"> <Input Type="button" value=" 确 认 " onclick="if(CheckForm('LogForm')){LogForm.submit();}" style="width:80px;height:25px;background:#900a12;border:solid 0px #ffffff;font-size:13px;color:#ffffff;font-weight:bold;font-family:'Microsoft YaHei' ! important;"> <input type="hidden" id="KeepMyInfo" name="KeepMyInfo" value="1"> </div> </form> </div> <div style="clear:both;overflow:hidden;height:50px;"></div> <div style="overflow:hidden;text-align:center;font-size:12px;color:#333333">安邦咨询(ANBOUND)内部管理系统,&copy 2025</div> </div> </body> </html>
05-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值