首页 > 代码库 > 初学JS

初学JS

JS验证表单是否为空,若为空,显示弹窗,之后将焦点聚集在为空的表单上:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script language="JavaScript">
        function check() {
            if (loginForm.username.value =http://www.mamicode.com/="") {
                alert("用户名不能为空!");
                loginForm.username.focus();
                return false;
            } else if (loginForm.password.value =http://www.mamicode.com/="") {
                alert("密码不能为空!");
                loginForm.password.focus();
                return false;
            }
        }
    </script>
</head>
<body>
<form name="loginForm" method="post" action="">
    用户:<input type="text" name="username"/>
    密码:<input type="password" name="password"/>
    <input type="submit" name="submit" value="http://www.mamicode.com/登录" onclick="return check();">
</form>
</body>
</html>

 JS制作二级导航菜单:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/styles.css">
    <script language="JavaScript">
        function Lmenu(value) {
            switch (value) {
                case ‘收藏‘:
                    submenu.innerHTML = "<a href=http://www.mamicode.com/‘#‘>宝贝收藏|店铺收藏";
                    break;
                case ‘已购买的宝贝‘:
                    submenu.innerHTML = "<a href=http://www.mamicode.com/‘#‘>购买列表|购买详情";
                    break;
                case ‘购物车‘:
                    submenu.innerHTML = "<a href=http://www.mamicode.com/‘#‘>清空购物车|填写订单信息";
                    break;
                case ‘个人中心‘:
                    submenu.innerHTML = "<a href=http://www.mamicode.com/‘#‘>个人资料|我的好友";
                    break;
            }
        }
    </script>
</head>
<body>
<div>
    <ul>
        <li><a href="http://www.mamicode.com/#" onmouseover="Lmenu(‘收藏‘);">收藏</a></li>
        <li><a href="http://www.mamicode.com/#" onm ouseover="Lmenu(‘已购买的宝贝‘);">已购买的宝贝</a></li>
        <li><a href="http://www.mamicode.com/#" onm ouseover="Lmenu(‘购物车‘);">购物车</a></li>
        <li><a href="http://www.mamicode.com/#" onm ouseover="Lmenu(‘个人中心‘);">个人中心</a></li>
    </ul>
</div>
<div id="submenu" class="submenu"></div>
</body>
</html>

 

初学JS