2014-02-20 81 views
0

你好,我有这个代码的问题:JS工作在浏览器中,但不是的jsfiddle

$("#col3, #col4").betterMouseover(500, function() { 
    var color; 
    if ($(this).attr('id') == "col3") { color = "#44a0ff"; } 
    else { color = "red"; } 
    $("#better").css({backgroundColor:""+color+""}); 
}); 


(function($) { 
     $.fn.betterMouseover = function (accidentTime, funkcia) { 
      if (accidentTime == undefined || accidentTime == null) { accidentTime = 250; } 
      if (funkcia == undefined || funkcia == null || typeof funkcia != 'function') { 
       funkcia = function() { 
        console.log("no callback action specified for betterMouseover()"); 
       }; 
      } 
      return this.each(function() { 
       var jeOut; 
       $(this).mouseenter(function() { 
        var totok = this; 
        jeOut = false; 
        setTimeout(function(){ 
         if (!jeOut) { $(totok).betterMouseoverHandler(funkcia); } 
        }, accidentTime); 
       }).mouseleave(function() { 
        jeOut = true; 
       }); 
      }); 
     } 
     $.fn.betterMouseoverHandler = function (fx) { 
      fx.call(this); 
     } 
}(jQuery)); 

JSFiddle link

代码在浏览器中,但不是的jsfiddle。这是我的小jQuery插件maden,我在第9行得到错误,说什么Object对象没有方法betterMouseover。 在JSFiddle中是否有一些限制,所以我无法运行jquery插件?

+2

你意识到你想存在之前使用的插件,对不对?你在jsfiddle上得到同样的错误。 '警报(FOO); var foo =“bar”;'会给你'undefined'。 –

回答

2

查看修订后的JSFiddle

您正在尝试在定义事件处理程序之前绑定新的插件事件处理程序。将事件处理程序脚本面板像这样的顶部:

脚本面板:

/* Define New Handler */ 
(function($) { 
    $.fn.betterMouseover = function (accidentTime, funkcia) { 
     if (accidentTime == undefined || accidentTime == null) { 
      accidentTime = 250; 
     } 
     if (funkcia == undefined || funkcia == null || typeof funkcia != 'function') { 
      funkcia = function() { 
       console.log("no callback action specified for betterMouseover()"); 
      }; 
     } 
     return this.each(function() { 
      var jeOut; 
      $(this).mouseenter(function() { 
      var totok = this; 
       jeOut = false; 
       setTimeout(function(){ 
        if (!jeOut) { 
         $(totok).betterMouseoverHandler(funkcia); 
        } 
       }, accidentTime); 
      }).mouseleave(function() { 
       jeOut = true; 
      }); 
     }); 
    } 
    $.fn.betterMouseoverHandler = function (fx) { 
     fx.call(this); 
    } 
}(jQuery)); 

/* Bind New Handler */ 
$("#col1, #col2").mouseenter(function() { 
    var color; 
    if ($(this).attr('id') == "col1") { 
     color = "#44a0ff"; 
    } else { 
     color = "red"; 
    } 
    $("#original").css({backgroundColor:""+color+""}); 
}); 


$("#col3, #col4").betterMouseover(500, function() { 
    var color; 
    if ($(this).attr('id') == "col3") { 
     color = "#44a0ff"; 
    } else { 
     color = "red"; 
    } 
    $("#better").css({backgroundColor:""+color+""}); 
}); 
+0

OMG这样一个愚蠢的错误:D谢谢:D – Kovo

+0

@Kovo没问题的哥们。祝你好运,快乐的编码! :) – War10ck

相关问题