zoukankan      html  css  js  c++  java
  • js 阻止浏览器默认行为

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>test</title>
    </head>
    <body>
    <a href="javascript:;">aa</a>
    <a href="" onclick="return false;">bb</a>


    <!-- return false只有在行内标签内才起作用-->
    <form action="http://www.baidu.com" onsubmit="return false;">
    用户名:
    <input type="text">
    <input type="submit"></form>

    <!-- 行内是函数时return false的用法 -->
    <form action="http://www.baidu.com" onsubmit="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.onsubmit=function(event){
    var oTex2=document.getElementById("tex2").value;
    if (!oTex2) {
    event.preventDefault();
    }
    }
    </script>
    </html>

  • 相关阅读:
    bootstrap
    Bootstrap面试题
    h5+css3 考试题
    less安装与使用
    css3(四)响应式 Web 设计 - 媒体查询
    css3(三)弹性布局
    css3(三)多列布局
    JavaScriptAPI初识
    Bootstrap面试题
    BS初识
  • 原文地址:https://www.cnblogs.com/chabai/p/5368909.html
Copyright © 2011-2022 走看看