2011-05-24 130 views
33

這裏是我的代碼:jQuery的 - 不是一個函數錯誤

(function($){ 
    $.fn.pluginbutton = function (options) { 
     myoptions = $.extend({ left: true }); 
     return this.each(function() { 
      var focus = false; 
      if (focus === false) { 
       this.hover(function() { 
        this.animate({ backgroundPosition: "0 -30px" }, { duration: 0 }); 
        this.removeClass('VBfocus').addClass('VBHover'); 
       }, function() { 
        this.animate({ backgroundPosition: "0 0" }, { duration: 0 }); 
        this.removeClass('VBfocus').removeClass('VBHover'); 
       }); 
      } 
      this.mousedown(function() { 
       focus = true 
       this.animate({ backgroundPosition: "0 30px" }, { duration: 0 }); 
       this.addClass('VBfocus').removeClass('VBHover'); 
      }, function() { 
       focus = false; 
       this.animate({ backgroundPosition: "0 0" }, { duration: 0 }); 
       this.removeClass('VBfocus').addClass('VBHover'); 
      }); 
     }); 
    } 
}); 


$(document).ready(function() { 
    $('.smallTabsHeader a').pluginbutton(); 
}); 

它給了我一個錯誤。怎麼了?

+1

什麼和哪裏是確切的錯誤? – 2011-05-24 11:46:34

回答

74

這個問題是 「最好的」 通過使用anonymous function解決了通jQuery對象中正是如此:

匿名函數如下:

<script type="text/javascript"> 
    (function($) { 
     // You pass-in jQuery and then alias it with the $-sign 
     // So your internal code doesn't change 
    })(jQuery); 
</script> 

這是JavaScript與'模式模式'一起使用時實現(窮人)'依賴注入'的方法。

所以你的代碼是這樣:
當然,你可能要做出一些改變你的內部代碼了,但你的想法。

<script type="text/javascript"> 
    (function($) { 
     $.fn.pluginbutton = function(options) { 
      myoptions = $.extend({ left: true }); 
      return this.each(function() { 
       var focus = false; 
       if (focus === false) { 
        this.hover(function() { 
         this.animate({ backgroundPosition: "0 -30px" }, { duration: 0 }); 
         this.removeClass('VBfocus').addClass('VBHover'); 
        }, function() { 
         this.animate({ backgroundPosition: "0 0" }, { duration: 0 }); 
         this.removeClass('VBfocus').removeClass('VBHover'); 
        }); 
       } 
       this.mousedown(function() { 
        focus = true 
        this.animate({ backgroundPosition: "0 30px" }, { duration: 0 }); 
        this.addClass('VBfocus').removeClass('VBHover'); 
       }, function() { 
        focus = false; 
        this.animate({ backgroundPosition: "0 0" }, { duration: 0 }); 
        this.removeClass('VBfocus').addClass('VBHover'); 
       }); 
      }); 
     } 
    })(jQuery); 
</script> 
5

當不同的系統抓取$變量時出現問題。您有多個$變量被用作來自多個庫的對象,導致錯誤。

爲了解決這個問題,只是你(function($){之前使用jQuery.noConflict

jQuery.noConflict(); 
(function($){ 
$.fn.pluginbutton = function (options) { 
... 
+0

firefox返回$(「。smallTabsHeader a」)。pluginbutton不是一個函數 – 2011-05-24 11:49:04

+0

我認爲(函數($){解決了你所描述的錯誤 – 2011-05-24 11:49:27

+0

以上在整個頁面導致大量錯誤 – 2011-05-24 11:52:52

7

變化

}); 


$(document).ready(function() { 
    $('.smallTabsHeader a').pluginbutton(); 
}); 

})(jQuery); //<-- ADD THIS 


$(document).ready(function() { 
    $('.smallTabsHeader a').pluginbutton(); 
}); 

這是必需的,因爲你需要調用的匿名函數你用

創建
(function($){ 

並注意到它期望它將在內部使用的參數爲$,所以您需要將引用傳遞給jQuery對象。

此外,您將需要改變所有的this.$(this).,除了第一個,在你做return this.each

在第一個(,你不需要$()),這是因爲在插件主體this持有對與您的選擇器匹配的jQuery對象的引用,但在任何深處,this都引用特定的DOM元素,因此您需要將其包裝在$()中。

的完整代碼在http://jsfiddle.net/gaby/NXESk/

3

我通過重命名我的函數來解決它。

更改

function editForm(value) 

function editTheForm(value) 

完美。

+11

wtf?這怎麼可能? – Mego 2016-11-02 13:26:51

2

當一個ASP.Net Web窗體原型轉換爲MVC的網站我得到了這些錯誤:

TypeError: $(...).accordion is not a function
$("#accordion").accordion(


$('#dialog').dialog({
TypeError: $(...).dialog is not a function

它在web表單工作正常。問題/解決方案是_Layout.cshtml中的此行

@Scripts.Render("~/bundles/jquery") 

註釋掉它,看看錯誤是否消失。然後修復它在BundlesConfig:

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js")); 
0

在我的情況,同樣的錯誤有一個更容易修復。基本上我的功能是在一個.js文件中,它沒有包含在當前顯示的aspx中。我所需要的只是包裝線。

相關問題