【问题标题】:how do i split these two Jquery functions properly?如何正确拆分这两个 Jquery 函数?
【发布时间】:2021-08-05 05:23:46
【问题描述】:

我想要做的是,给定以下脚本:

 (function($) {
  $.fn.Button_Activator = function() {
        return this.each(function() {
          var coll = document.getElementsByClassName("collapsible");
          var i;
          for (i = 0; i < coll.length; i++) {
              coll[i].addEventListener("click", function() {
              this.classList.toggle("active");
              var content = this.nextElementSibling;
              if (content.style.maxHeight){
                content.style.maxHeight = null;
              } else {
                content.style.maxHeight = content.scrollHeight + "px";
              }
            });
          }
        });
    };
})(jQuery); // End of use strict

我试图将它拆分为两个单独文件中的两个函数,第一个是:“little_one.js”

(function ($) {
    $.fn.little_one = function (test) {
        return this.each(function (test) {
            this.classList.toggle("active");
            var content = this.nextElementSibling;
            if (content.style.maxHeight) {
                content.style.maxHeight = null;
            } else {
                content.style.maxHeight = content.scrollHeight + "px";
            };
        });
    };
    
})(jQuery); // End of use strict

第二个是“Button_Activator.js”:

(function ($) {
    $.fn.Button_Activator = function () {
        return this.each(function () {
            var coll = document.getElementsByClassName("collapsible");
            var i;
            for (i = 0; i < coll.length; i++) {
                coll[i].addEventListener("click", little_one(coll[i]));
            }
        });
    };
})(jQuery); // End of use strict

现在,当我使用以前的函数时会发生什么情况是浏览器显示“未捕获的 ReferenceError:little_one 未定义”以及“jquery.min.js:2 jQuery.Deferred 异常:未定义 little_one ReferenceError:little_one没有定义”。 现在,在 HTML 中,我已经验证了它在 Button_Activator.js 之前导入 little_one.js,因此我最好的猜测是拆分的实现有问题。任何对此事有任何意见的人都会非常感激。

【问题讨论】:

    标签: javascript jquery jquery-plugins


    【解决方案1】:

    我可以看到代码 sn-p 的一些问题,

    1. little_one 存在于 jquery $ 对象上,因此您需要引用它
    coll[i].addEventListener("click", $().little_one(coll[i]));
    
    1. addEventListener 需要一个函数引用作为它的第二个参数,但您实际上所做的是立即调用该函数并返回结果,因此您需要传递函数引用
    coll[i].addEventListener("click", $().little_one);
    
    1. this.each 存在于 jQuery 元素,而不是 DOM 元素,一般来说,我建议不要将 DOM 元素与 jQuery 元素混合,因为很难跟踪哪个是哪个。
    (function ($) {
        $.fn.little_one = function () {
            this.classList.toggle("active");
            var content = this.nextElementSibling;
            if (content.style.maxHeight) {
                content.style.maxHeight = null;
            } else {
                content.style.maxHeight = content.scrollHeight + "px";
            };
        };
    })(jQuery); // End of use strict
    

    工作 jsFiddle:http://jsfiddle.net/76yumo4n/

    【讨论】:

    • 谢谢你,真的,你澄清了我对这件事的很多疑惑。
    猜你喜欢
    • 2023-03-17
    • 2011-03-12
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多