老师,没有效果

来源:4-15 编程练习

Miracle777

2019-09-12 09:53:31

<!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();

            }

        });

        //此处写代码

        var submitButton=document.getElementById("submits");

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

            var inValidityField=form.querySelectorAll(":invalid"),

            errorMessage=form.querySelectorAll(".error-message"),parent;

        }

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

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

        }

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

            parent=inValidityField[i].parentNode;

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

        }

        if (invalidFields.length > 0) {

            invalidFields[0].focus();

        }

    }

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

    replaceValidationUI(forms);

    </script>

</body>


</html>


写回答

1回答

好帮手慕糖

2019-09-12

同学你好,1、循环前,不需要花括号。if语句后面,才应该是结束点击事件的花括号,后面还需要有一个小括号哦,例:

http://img1.sycdn.imooc.com/climg/5d79e8670969e2e011700875.jpg

2、定义的变量是inValidityField,所以if语句这里也应该使用inValidityField哦,例:

http://img1.sycdn.imooc.com/climg/5d79e70809b55d3605710258.jpg

3、js中设置的类,是error-message,没有最后一个s,所以样式这里可以去掉,例:

http://img1.sycdn.imooc.com/climg/5d79e8c909a04b2504210246.jpg

4、修改之后,运行效果如下:

http://img1.sycdn.imooc.com/climg/5d79e9d709f4626f06240178.jpg

与如下效果图还是有点不一致:

http://img1.sycdn.imooc.com/climg/5d79ea0009be848d03990107.jpg

所有错误信息应该在最前面,并且错误信息前面还有提示是哪个输入框的信息,可以参考如下建议:

(1)可以在往上找一级父节点,找到form标签之后再插入,如下:

http://img1.sycdn.imooc.com/climg/5d79ea8a09a6b74009670240.jpg

(2)使用afterbegin,在前面插入;

http://img1.sycdn.imooc.com/climg/5d79eab4094422d712530376.jpg

(3)、错误信息提示前面的名字,可以通过父级来获取label元素,然后将label元素的内容拼接到错误信息上(这样是没有提示信息前面的小圆点的,可以尝试自己添加下哦):

http://img1.sycdn.imooc.com/climg/5d79ecec095e7ec013030406.jpg

希望能帮助到你,欢迎采纳。

祝学习愉快!

0

0 学习 · 5012 问题

查看课程