<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>test</title></head><body> <a href="javascript:;">aa</a> <a href="" οnclick="return false;">bb</a>
<!-- return false只有在行内标签内才起作用--> <form action="http://www.baidu.com" οnsubmit="return false;"> 用户名: <input type="text"> <input type="submit"></form>
<!-- 行内是函数时return false的用法 --> <form action="http://www.baidu.com" οnsubmit="return check()" style="margin-top: 20px;"> 密码: <input type="text" id="tex1"> <input type="submit"></form>
<!-- 在标签外给标签加事件 ,,这时也可以用return false 但最好用event.preventDefault();--> <form action="http://www.baidu.com" id="form1" style="margin-top: 20px;"> 校验: <input type="text" id="tex2"> <input type="submit"></form>
</body><script> function check(){ var oTex=document.getElementById('tex'); if(oTex.value){ return ture; }else{ event.preventDefault(); } }</script>
<script> var oForm=document.getElementById('form1'); oForm.οnsubmit=function(event){ var oTex2=document.getElementById("tex2").value; if (!oTex2) { event.preventDefault(); } }</script></html>
转载于:https://www.cnblogs.com/chabai/p/5368909.html
相关资源:js阻止浏览器默认行为的简单实例