4-15编程练习

来源:4-15 编程练习

soso_crazy

2019-02-09 18:46:43

<!DOCTYPE html>

<html>


<head>

    <meta charset="UTF-8">

    <title></title>

    <style>

        .oneline {

            line-height: 1.5;

            margin: 10px auto;

        }

        

        .oneline label {

            width: 100px;

            text-indent: 15px;

            font-size: 14px;

            font-family: "Microsoft Yahei";

            display: inline-block;

        }

        

        .oneline .sinput {

            width: 60%;

            height: 30px;

            border-radius: 6px;

            border: 1px solid #e2e2e2;

        }

        

        .oneline input[type="submit"] {

            margin-left: 20px;

            width: 80px;

            height: 30px;

            border: 0;

            background-color: #5899d0;

            color: #fff;

            font-size: 14px;

            border-radius: 6px;

        }

        

        .error-messages {

            color: red;

        }

    </style>

</head>

<body>

    <form id="forms">

        <div class="oneline">

            <label for="name">用户名:</label>

            <input id="name" class="sinput" name="name" type="text" required>

        </div>

        <div class="oneline">

            <label for="email">Email:</label>

            <input id="email" class="sinput" name="email" type="email" required>

        </div>

        <div class="oneline">

            <input type="submit" id="submits" value="提交">

        </div>

    </form>

    <script>

    function replaceValidationUI(form) {

        form.addEventListener("invalid", function(event) {

            event.preventDefault();

        }, true);

        form.addEventListener("submit", function(event) {

            if (!this.checkValidity()) {

                event.preventDefault();

            }

        },true);

        //此处写代码

        var submitButton=ducument.getElementById('submits');

        submitButton.addEventListener('click',function(event){

            var inValidityFalse=form.querySelectorAll(:invalid),

            errorMessage=form.querySelectorAll('.errorMessage');

        })

        

        for(var i=0;i<errorMessage.length;i++){

            errorMessage[i].parentNode.removeChild(errorMessage[i]);

        }

        

        for(var i=0;i<inValidityFalse.length;i++){

            parent=inValidityFalse[i].parentNode;

            parent.insertAdjacentHTML('afterbegin',"<div class='error-message'>" + inValidityFalse[i].validationMessage + "</div>")

        }

        

        if (invalidFields.length > 0) {

            invalidFields[0].focus();

        }

    }

    var forms = document.getElementById("forms");

    replaceValidationUI(forms);

    </script>

</body>


</html>

为什么无法实现要求?

写回答

1回答

好帮手慕夭夭

2019-02-11

你好同学 , 问题与修改如下:

http://img.mukewang.com/climg/5c6121ee00019f4810200401.jpg\

http://img.mukewang.com/climg/5c61221f00010f3605140141.jpg

http://img.mukewang.com/climg/5c61222a0001dea406340258.jpg

另外 ,根据效果图 ,提示信息要在最前面 ,并且文字是红色的 ,如下:

http://img.mukewang.com/climg/5c61229600012d9912170136.jpg

http://img.mukewang.com/climg/5c6122b80001b68605380116.jpg

祝学习愉快 ,望采纳 .

0

0 学习 · 5012 问题

查看课程

相似问题