JavaScript插件多次启动

JavaScript插件多次启动,javascript,for-loop,for,Javascript,For Loop,For,所以我有一个javascript插件 ;(function(window){ //some functions function plugin(el, options){ } plugin.prototype = { //other functions } })(window); 然而,这并不重要。 然后我这样称呼这个插件 document.addEventListener('DOMContentLoaded', function() { var el = document.querySe

所以我有一个javascript插件

;(function(window){
//some functions
function plugin(el, options){
}
plugin.prototype = {
//other functions
}
})(window);
然而,这并不重要。
然后我这样称呼这个插件

document.addEventListener('DOMContentLoaded', function() {
  var el = document.querySelectorAll('[data-tooltip]')
  for(i = 0; i < el.length; i++){
    new tooltipizeIt(el[0], { effect : 'fade-and-move', theme : 'custom', width : 200, position : 'right'});
    new tooltipizeIt(el[1], { theme : 'light', effect : 'fade', width : 120, position : 'top'});
    new tooltipizeIt(el[2], { effect : 'scale', theme : 'dark', position : 'bottom'});
    new tooltipizeIt(el[3], { effect : 'no-effect', theme : 'custom', position : 'top', width : 135});
    new tooltipizeIt(el[4], { effect : 'fade-and-move', theme : 'light', position : 'bottom', width : 400});
    new tooltipizeIt(el[5], { effect : 'fade-and-move', theme : 'light', position : 'left', width : 400, ajax : true});
  }
}, false);
document.addEventListener('DOMContentLoaded',function(){
var el=document.querySelectorAll(“[data tooltip]”)
对于(i=0;i
正如你们所看到的,我六次调用这个插件,但插件对每个
el
元素都会触发六次,如果我调用七次,就会触发七次,以此类推。
有人知道为什么,是否有解决方案

编辑 我添加了一个图像,正如您所看到的,插件为同一个元素创建了六个工具提示

我同意@adeneo的说法,它正在做你让它做的事情。 由于您的循环,它将始终进行
6*el.length
调用

具体来说,带有
数据工具提示的前6个元素中的每一个都会有6次相同的调用。这就是循环的工作方式

例如,第一个元素将使此特定调用发生6次:

new tooltipizeIt(el[0], { effect : 'fade-and-move', theme : 'custom', width : 200, position : 'right'});
new tooltipizeIt(el[1], { theme : 'light', effect : 'fade', width : 120, position : 'top'});
而第二个将有此特定的所有发生6次:

new tooltipizeIt(el[0], { effect : 'fade-and-move', theme : 'custom', width : 200, position : 'right'});
new tooltipizeIt(el[1], { theme : 'light', effect : 'fade', width : 120, position : 'top'});
等等。如果有第七个元素,它将根本不会被调用。同样,如果少于6个元素属性,它将失败,因为您直接访问每个元素属性,并且
el[5]
未定义

这里演示了这类事情是如何工作的,但是需要更多的细节才能真正让代码实现您想要的功能。希望将此代码与输出进行比较将有助于您理解此处涉及的概念

//这是一个显示某些输出的简单调用
功能日志(消息){
$(“#日志”).text($(“#日志”).text()+消息+'\n');
}
//
//这是真正的东西
//
//创建一个包含六个值的数组,非常类似于您的示例。
变量el=['A','B','C','D','E','F'];
日志(“el中有“+el.length+”项。”)
//循环获取数组中的所有项。
对于(变量i=0;i


为什么不呢,您正在迭代元素,为每个元素调用它六次?如果我像这样调用它,{effect:'fade And move',theme:'custom',width:200,position:'right'}),对于每个元素它将触发一次,但例如,如果我想为tw元素调用它,例如
newtooltipizeit(el[0],{//some options'});新的工具提示(el[1],{//其他一些选项})首先使用一些选项,第二个使用另一个选项,它将为每个