我写的报错了

来源:4-13 html5默认气泡修改演示

人艰不拆OMG

2018-09-17 15:46:40

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>Document</title>

    <style>

        .oneline{

        line-height: 1.5;

        margin: 10px auto;

        }

        .oneline label{

        display: inline-block;

        width: 100px;

        text-indent: 15px;

        font-size:14px;

        font-family: "Microsoft Yahei";

        }

        .oneline .sinut{

        border: 1px solid #e2e2e2;

        border-radius: 3px;

        width: 60%;

        height: 30px;

        line-height: 40px;

        }

        .oneline input[type="submit"]{

        width: 80px;

        background: #5899d0;

        color: #fff;

        height: 30px;

        border: 0;

        border-radius: 5px;

        margin-left: 20px;

        font-size: 14px;

        }

        .error-message{

        color:red; 

        font-size:12px;

        text-indent:108px;

        }

    </style>

</head>

<body>

    <form class="forms">

    <div class="oneline">

    <label>用户名:</label>

    <input type="text" name="name" class="sinut" id="name" required/>

    </div>

    <div class="oneline">

    <label>Email:</label>

    <input type="email" name="email" class="sinut" id="email"  required/>

    </div>

    <div class="oneline">

    <input type="submit" id="thesubmit"/>

    </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 submitBtn=document.getElementById('thesubmit');

   

    submitBtn.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.removeNode(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 (inValidityField.length>0) {

           inValidityField[0].focus();

       }

   

    });

   

    }

   

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

replaceValidationUI(form);

     

    </script>

    

</body>

</html>

http://img.mukewang.com/climg/5b9f5bc40001e1dd12420091.jpg求助老师,这是哪的问题我看不出来了

写回答

1回答

好帮手慕星星

2018-09-17

1、html中设置的是class,js中获取的是id。

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

2、移除错误信息时,方法错了

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

自己修改测试下,祝学习愉快~~

0

0 学习 · 5012 问题

查看课程