简体   繁体   English

如何编写和/或调用jQuery插件?

[英]How to write and/or call a jQuery plug-in?

I'm trying to follow this article to write a simple jQuery plugin: http://brolik.com/blog/how-to-create-a-jquery-plugin/ 我正在尝试按照本文编写一个简单的jQuery插件: http : //brolik.com/blog/how-to-create-a-jquery-plugin/

I seem to always get the following error in my console: 我的控制台似乎总是出现以下错误: 未捕获的TypeError:$(...)。blogPost不是函数

HTML 的HTML

<div id="prod-part">TODO write content</div>   

Javascript Java脚本

(function($){
    $.blogPost = function(el, options) {
        var base = this;

        base.$el = $(el);
        base.el = el;

        base.$el.data('blogPost', base);

        base.init = function(){
            console.log("hello");
        };
    };
})(jQuery);

$(function () {
    $('#prod-part').blogPost();

});

Here is a simple jsfiddle which still creates the issue. 这是一个仍然会产生问题的简单jsfiddle。 I'm not sure If I am calling on the plug-in incorrectly or if the plugin is coded incorrectly. 我不确定我是不正确地调用插件还是插件编码不正确。 I've tried jQuery versions 1.7.2 and 1.11.0 and still come out with the same results. 我已经尝试过jQuery 1.7.2和1.11.0版本,但仍然得出相同的结果。 Any suggestions would be greatly appreciated. 任何建议将不胜感激。 http://jsfiddle.net/45oLp31m/1/ http://jsfiddle.net/45oLp31m/1/

Background: 背景:

The jQuery function ( jQuery() or $() ) acts as a factory to return a new instance of jQuery collection when you pass in a selector. 当您传入选择器时,jQuery函数( jQuery()$() )充当返回jQuery集合新实例的工厂。 So $('#foo') returns an instance of jQuery collection. 因此$('#foo')返回jQuery集合的实例。 In order to call methods off of that instance, like $('#foo').somePlugin() , those methods have to be defined on the instance. 为了从该实例中调用方法,例如$('#foo').somePlugin() ,必须在实例上定义这些方法。 The primary way we get methods onto instances is to add them to the constructor's prototype. 我们将方法获取到实例的主要方法是将它们添加到构造函数的原型中。

Solution

So the solution to your specific error is that jQuery plugins are defined on the jQuery collection constructor's prototype. 因此,针对您的特定错误的解决方案是在jQuery集合构造函数的原型上定义jQuery插件。 This prototype is aliased at jQuery.fn (and jQuery is aliased as $ , so $.fn is also ok). 该原型的别名为jQuery.fn (而jQuery的别名为$ ,因此$.fn也可以)。 Adding methods to the prototype is as simple as $.fn.somePlugin = function () {} . 向原型添加方法就像$.fn.somePlugin = function () {}一样简单。

Thus your plugin needs to be defined like: 因此,您的插件需要定义为:

$.fn.blogPost = function(el, options) {

More 更多

As I said, this is for the specific error you quoted. 如我所说,这是针对您所引用的特定错误。 I assume at this point that you haven't made it much further in your tutorial, so I won't go into the other issues in your code (like the fact that your plugin does not return the collection for chaining). 我假设此时您还没有在教程中做得更深入,所以我不会涉及代码中的其他问题(例如您的插件不会返回用于链接的集合的事实)。

Instead of 代替

$.blogPost = function(el, options) {...}

Try 尝试

$.fn.blogPost = function(el, options) {...}

Functions in the $.fn namespace are available as methods on jQuery collections, which is what you want. $.fn名称空间中的函数$.fn jQuery集合上的方法,这就是您想要的。

$(selector).blogPost();

Such methods a generally known as "plugins". 这种方法通常称为“插件”。

In other (rarer) circumstances, you might want to extend the jQuery namespace itself, in which case your $.foo = function() {...} would be applicable. 在其他(很少)情况下,您可能想扩展jQuery命名空间本身,在这种情况下, $.foo = function() {...}将适用。 Such functions are known as "static methods". 这种功能被称为“静态方法”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM