小编Jac*_*ine的帖子

Array.prototype.filter.call(forms, function(form) 这是在做什么?

我很难完全理解这段代码的工作原理。这只是从引导程序复制和粘贴的表单验证代码。

我的问题从这一行开始 var validation = Array.prototype.filter.call(forms, function(form)

在我看来,它正在创建一个名为“validation”的数组,其中包含任何类名为“needs-validation”的元素。然后是调用匿名函数并传入整个表单并运行后续代码行

 form.addEventListener('submit', function(event) {
     if (form.checkValidity() === false) {
     event.preventDefault();
     event.stopPropagation();
     } 
Run Code Online (Sandbox Code Playgroud)

在包含类名“需要验证”的每个元素上?

<script>
    // Example starter JavaScript for disabling form submissions if there are invalid fields
    (function() {
      'use strict';
      window.addEventListener('load', function() {
        // Fetch all the forms we want to apply custom Bootstrap validation styles to
        var forms = document.getElementsByClassName('needs-validation');
        // Loop over them and prevent submission
        var validation = Array.prototype.filter.call(forms, function(form) {
      form.addEventListener('submit', function(event) {
        if (form.checkValidity() === false) { …
Run Code Online (Sandbox Code Playgroud)

html javascript bootstrap-4

4
推荐指数
1
解决办法
1694
查看次数

标签 统计

bootstrap-4 ×1

html ×1

javascript ×1