h5自带表单默认气泡修改
Posted jian1234
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了h5自带表单默认气泡修改相关的知识,希望对你有一定的参考价值。
<!DOCTYPE html>
<html lang="en">
<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 afterB = document.getElementById(‘forms‘);
afterB.addEventListener(‘click‘ , function(event){
var inValidAll = form.querySelectorAll(‘:invalid‘), //获取所有不符合验证的信息
errorAll = form.querySelectorAll(‘.error-messages‘), //获取所有错误的信息
after;
for(var i = 0;i<inValidAll.length;i++){
after = inValidAll[i].parentNode;
afterB.insertAdjacentHTML(‘afterbegin‘,‘<ul class="error-messages"><li>用户名:‘+inValidAll[i].validationMessage+‘</li></ul>‘);
}
if (inValidAll.length > 0) {
inValidAll[0].focus();
}
for(var i = 0;i<errorAll.length;i++){
errorAll[i].parentNode.removeChild(errorAll[i])
}
})
}
var forms = document.getElementById("forms");
replaceValidationUI(forms);
</script>
</body>
</html>
以上是关于h5自带表单默认气泡修改的主要内容,如果未能解决你的问题,请参考以下文章