4-15编程练习

来源:4-15 编程练习

soso_crazy

2019-02-12 11:21:15

<!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  invalidFields=form.querySelectorAll(':invalid'),
            errorMessage=form.querySelectorAll('.error-messages'),
            parent;
            
        for(var i=0;i<errorMessage.length;i++){
            errorMessage[i].parentNode.removeChild(errorMessage[i]);
           }
           
           
        for(var i=0;i<invalidFields.length;i++){
          parent=invalidFields[i].parentNode.parentNode;
          parent.insertAdjacentHTML('afterbegin',"<div class='error-messages'>"+inValidityField[i].validationMessage+ "</div>");
        }
        
         if (invalidFields.length > 0) {
            invalidFields[0].focus();
        }
    })
    var forms = document.getElementById("forms");
    replaceValidationUI(forms);
    
    
    </script>
</body>

</html>
  1. 为什么不能实现,问题出在哪?

  2. 麻烦老师讲讲思路,不太明白每一段JS代码的意思

  3. for(var i=0;i<invalidFields.length;i++){
             parent=invalidFields[i].parentNode.parentNode;
             parent.insertAdjacentHTML('afterbegin',"<div class='error-messages'>"+inValidityField[i].validationMessage+ "</div>");
           }为什么要找父节点的父节点?有什么意义?详细讲解这一段

写回答

1回答

好帮手慕星星

2019-02-12

你好,可以看报错信息:

1、http://img.mukewang.com/climg/5c6245370001ebd605100032.jpg

报错信息:不期望的结尾输入

代码中没有与replaceValidationUI函数相匹配的结尾花括号,可以添加下:

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

2、点击提交按钮之后,报错如下:

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

可以到相应函数去找错误的地方:

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

按照上面修改之后,测试是正确的:

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

建议在前面添加上名称信息,如效果图:

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

这样看起来会比较清晰,可以自己完善添加下。

3、可以看下面图片中的解释进行理解:

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

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

4、invalidFields[i].parentNode.parentNode;找到的父节点为form元素,是为了把错误信息插入在所有内容的最前面:

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

自己可以测试理解下,祝学习愉快!

0

0 学习 · 5012 问题

查看课程

相似问题