欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

javascript练习

程序员文章站 2022-04-02 10:41:57
...

练习一:进行用户注册的验证

创建一个用户注册的验证脚本,手机号码需要负责规范,密码符合规定,并且输入验证码。效果如图:
javascript练习

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .error {
            outline: 2px solid red;
        }

        .success {
            outline: 2px solid green;
        }

        .red {
            color: red;
        }

        .green {
            color: green;
        }

        #show {
            font-size: 30px;
            width: 100px;
            height: 61px;
            background: greenyellow;
            text-align: center;
            line-height: 61px;
            margin: 10px;
            border-radius: 5px;

        }
    </style>
</head>
<body>
<form action="">
    <label for="usename">用户名</label>
    <input type="text" id="usename">
    <span id="s1"></span>
    <br>
    <label for="password">密码</label>
    <input type="password" id="password">
    <span id="s2"></span>
    <br>
    <label for="qr">确认密码</label>
    <input type="password" id="qr">
    <span id="s3"></span>
    <br>
    <label for="yzm">验证码</label>
    <input type="text" id="yzm">
    <span id="s4"></span>
    <div id="show"></div>
    <input type="submit" id="tijiao">
</form>
<script>
    var usename = document.getElementById("usename");
    var password = document.getElementById("password");
    var qr = document.getElementById("qr");
    var yzm = document.getElementById("yzm");
    var show = document.getElementById("show");
    var tijiao = document.getElementById("tijiao");
    var s1 = document.getElementById("s1");
    var s2 = document.getElementById("s2");
    var s3 = document.getElementById("s3");
    var s4 = document.getElementById("s4");
    tijiao.disabled = "disabled";

    usename.onblur = function () {
//        console.log(usename.value);
        var tel = usename.value;
        /* if (/^(13|14|15|17|18)[0-9]{9}$/.test(tel)) {
         usename.className = 'success';
         s1.className = 'green';
         s1.innerHTML = "正确";
         }else {
         usename.className = 'error';
         s1.className = 'red';
         s1.innerHTML = "请输入正确的手机号";
         }*/
        if (tel.length == 11) {
            var arr = ['13', '14', '15', '17', '18','19'];
            if (arr.indexOf(tel.substr(0, 2)) > -1) {
                usename.className = 'success';
                s1.className = 'green';
                s1.innerHTML = "正确";
                puanduan();
            } else {
                usename.className = 'error';
                s1.className = 'red';
                s1.innerHTML = "请输入正确的手机号";
            }
        } else {
            usename.className = 'error';
            s1.className = 'red';
            s1.innerHTML = "请输入正确的手机号";
        }
    };

    password.onblur = function () {
        var mima = password.value;
        if (mima.length >= 6 && mima.length <= 12) {
            password.className = 'success';
            s2.className = 'green';
            s2.innerHTML = "正确";
            if (mima == qr.value) {
                qr.className = 'success';
                s3.className = 'green';
                s3.innerHTML = "正确";
                puanduan();
            }
            puanduan();
        } else {
            password.className = 'error';
            s2.className = 'red';
            s2.innerHTML = "请输入6到12位的密码";
        }

        /* if(mima==qr.value&&password.value!=""){
         qr.className = 'success';
         s3.className = 'green';
         s3.innerHTML = "正确";
         }else {
         qr.className = 'error';
         s3.className = 'red';
         s3.innerHTML = "2次密码不一致";
         }*/

    };
    qr.onblur = function () {
        var qrmm = qr.value;
        if (qrmm == password.value && password.value != "") {
            qr.className = 'success';
            s3.className = 'green';
            s3.innerHTML = "正确";
            puanduan();
        } else {
            qr.className = 'error';
            s3.className = 'red';
            s3.innerHTML = "2次密码不一致";
        }

    };

    show.onclick = function () {
//        var arr = [1,2,3,4,5,6,....."a",'b'..."A"]
        var str = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM';
        var jieguo = "";
        for (var i = 0; i < 4; i++) {
            var num = Math.floor(Math.random() * str.length);
            jieguo = jieguo + str[num];
        }
        show.innerHTML = jieguo;
//        sc();
    };
    /*function sc() {
     var str = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM';
     var jieguo = "";
     for(var i=0;i<4;i++){
     var num = Math.floor(Math.random()*str.length);
     jieguo = jieguo+str[num];
     }
     show.innerHTML = jieguo;
     }
     sc();*/

    /*var obj  = {
     getName:function () {
     console.log(111);
     }
     };
     obj.getName();*/
    show.onclick();

    yzm.onblur = function () {
        if (yzm.value.toLocaleUpperCase() == show.innerHTML.toLocaleUpperCase()) {
            yzm.className = 'success';
            s4.className = 'green';
            s4.innerHTML = "正确";
            /*if(s1.innerHTML=="正确"&&s2.innerHTML=="正确"&&s3.innerHTML=="正确"){
             tijiao.disabled = "";
             }*/
            puanduan();
        } else {
            yzm.className = 'error';
            s4.className = 'red';
            s4.innerHTML = "请输入正确的验证码";
        }
    };

    function puanduan() {
        if (s1.innerHTML == "正确" && s2.innerHTML == "正确" && s3.innerHTML == "正确" && s4.innerHTML == "正确") {
            tijiao.disabled = "";
        }
    }


</script>
</body>
</html>

练习二:三级联动表

制作一个可以选择省市区的三级下拉选项框。选中一个之后,会自动选中后面的第一个。效果如图:

javascript练习

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>三级菜单</title>
    <style>
        select{
            width: 200px;
            height: 30px;
        }
        span{
            margin-left: 10px;
        }
    </style>
</head>
<body>

<span>省:</span>
<select class="province" name="" id="1">
    <option value="">请选择</option>
</select>
<span>市:</span>
<select class="town" name="" id="2"></select>
<span>区:</span>
<select class="area" name="" id="3"></select>


<script>
    /*
     1.option元素的创建:var option = new Option('要显示的内容');
     2.将option元素添加到select元素中:select.options.add(option);
     3.将select元素中的option清空:select.options.length = 0;
     4.select元素有一个叫做selectedIndex,用来表示用户选中的这个option的序号,从0开始
     5.change事件会在select元素的value值发生改变的时候,自动调用
     */
    var provinceSelect =document.querySelector('.province');
    var townSelect =document.querySelector('.town');
    var areaSelect =document.querySelector('.area');
    var provinceArray =['北京市','上海市','天津市','江西省','河北省','山西省','山东省','河南省','陕西省'];
    var townArray = [
        ['北京市'],
        ['上海市'],
        ['天津市'],
        ['南昌市','赣州市','景德镇市','抚州市']];
    var areaArray = [
        [['海淀区','朝阳区','大兴区']],
        [['静安区','宝山区','青浦区']],
        [['天津一区','天津二区','天津三区']],
        [['青浦区','高兴区','南昌镇'], ['赣州一区','赣州二区','赣州三区'],['昌江区','浮梁县','乐平市']]
    ];


    for (var i = 0;i<provinceArray.length;i++){
        var provinceOption = new Option(provinceArray[i]);
        provinceSelect.options.add(provinceOption);
    }
    var provinceIndex = 0;
    provinceSelect.onchange =function(eve){

        if (provinceIndex ==-1){
            townSelect.options.length=0;
            areaSelect.options.length=0;
        }
        else
        {
            townSelect.options.length=0;
            areaSelect.options.length=0;
            provinceIndex= eve.target.selectedIndex-1;
            for (var j=0;j<townArray[provinceIndex].length;j++){
                var townOption = new Option(townArray[provinceIndex][j]);
                townSelect.options.add(townOption);}
            for (var k=0;k<areaArray[provinceIndex][0].length;k++){
                var areaOption = new Option(areaArray[provinceIndex][0][k]);
                areaSelect.options.add(areaOption);
            }

        }

    };

    townSelect.onchange = function(eve){
        areaSelect.options.length=0;
        var townIndex = eve.target.selectedIndex;
        for (var k=0;k<areaArray[provinceIndex][townIndex].length;k++){
            var areaOption = new Option(areaArray[provinceIndex][townIndex][k]);
            areaSelect.options.add(areaOption);
        }

    }



</script>
</body>
</html>

练习三:放大镜

创建和淘宝的左边图的样子一样,鼠标悬停可以放大图片查看细节,效果如图:
javascript练习

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>lesson5</title>
    <style>
        *{  margin: 0; padding: 0;  }
        .small{
            width: 400px;  height: 400px;  background: url("img/cat.png") no-repeat;
            background-size: 100%;  float: left; position: relative;
        }
        .small .fangdajing{  width: 100px; height: 100px;
            background-color: rgba(160,86,38,0.5); position: absolute;
            display: none;
        }
        .big{
            width: 400px;  height: 400px;  background: orange;  float: left;
            overflow: hidden; position: relative; display: none;
        }
        img{
            position: absolute;
        }
    </style>
</head>
<body>

<div class="small">
    <div class="fangdajing"></div>
</div>
<div class="big">
    <img src="img/cat.png" width="2000" height="2000" alt=""/>
</div>

<script>
    var smallDiv = document.querySelector('.small');
    var fangdajingDiv = document.querySelector('.fangdajing');
    var bigDiv = document.querySelector('.big');
    var img = document.querySelector('img');

    smallDiv.onmousemove = function (eve) {
        /*获取鼠标坐标*/
        var mouseLeft = eve.clientX;
        var mouseTop = eve.clientY;
        /*设置边界*/
        if(mouseLeft<50){mouseLeft = 50;}
        if(mouseLeft>350){mouseLeft = 350;}
        if(mouseTop<50){mouseTop = 50;}
        if(mouseTop>350){mouseTop = 350;}
        /*让放大镜div跟随鼠标移动*/
        fangdajingDiv.style.left = mouseLeft - 50 + 'px';
        fangdajingDiv.style.top = mouseTop - 50 + 'px';
        fangdajingDiv.style.display = 'block';
        /*让大图跟随放大镜移动*/
        img.style.left = mouseLeft * (-5) + 'px';
        img.style.top = mouseTop * (-5) + 'px';
        bigDiv.style.display = 'block';
    };
    smallDiv.onmouseleave = function (eve) {
        fangdajingDiv.style.display = 'none';
        bigDiv.style.display = 'none';
    }



</script>
</body>
</html>
相关标签: html js