简体   繁体   English

如何创建简单的 jQuery 插件?

[英]How to create simple jQuery plugin?

This test plugin, is supposed to work like this: When an element is clicked, it moves down.这个测试插件,应该是这样工作的:当一个元素被点击时,它会向下移动。 Simple as that.就那么简单。

jQuery.fn.moveDown = function(howMuch){
    $(this).css("border", "1px solid black");
    $(this).click(function(){

        $(this).css("position", "relative");
        $(this).animate({top: '+='+howMuch});
    }); 
}

The problem is, when an element is clicked, it not only moves the clicked element but also ALL the other elements which the plugin was applied to.问题是,当一个元素被点击时,它不仅会移动被点击的元素,还会移动应用了插件的所有其他元素。

What is the solution for this?解决方案是什么?

For plugin authoring try this way, much more solid:对于插件创作,请尝试这种方式,更加可靠:

Edit: Here is working jsFiddle example.编辑:这是有效的 jsFiddle 示例。


PLUGIN:插入:

(function($){
    $.fn.extend({
        YourPluginName: function(options) {
                var defaults = {
                      howMuch:'600',
                      animation: '',//users can set/change these values
                      speed: 444,
                      etc: ''
                }
        };

       options = $.extend(defaults, options);

       return this.each(function() {
          var $this = $(this);              
          var button = $('a', $this);// this represents all the 'a' selectors;
                                            // inside user's plugin definition.

          button.click(function() {
            $this.animate({'top':options.howMuch});//calls options howMuch value
          });
       });
})(jQuery);

USER'S DOCUMENT:用户文档:

$(function() {
   $('#plugin').YourPluginName({
     howMuch:'1000' //you can give chance users to set their options for plugins
   });
});

<div id="plugin">
  <a>1</a>
  <a>2</a>
  <a>3</a>
</div>

Here i want to suggest steps to create simple plugin with arguments.在这里,我想建议创建带有参数的简单插件的步骤。

JS JS

(function($) {
    $.fn.myFirstPlugin = function( options ) {

        // Default params
        var params = $.extend({
            text     : 'Default Title',
            fontsize : 10,
        }, options);
        return $(this).text(params.text);

    }
}(jQuery));

Here, we have added default object called params and set default values of options using extend function.在这里,我们添加了名为params的默认对象,并使用extend函数设置了选项的默认值。 Hence, If we pass blank argument then it will set default values instead otherwise it will set.因此,如果我们传递空白参数,那么它将设置默认值,否则它将设置。

HTML HTML

$('.cls-title').myFirstPlugin({ text : 'Argument Title' });

Read more: How to Create JQuery plugin阅读更多:如何创建 JQuery 插件

Original answer Here i want to suggest steps to create simple plugin with arguments原始答案在这里,我想建议创建带参数的简单插件的步骤

If you have Node.js installed you can use create-jquery-plugin CLI utility.如果您安装了 Node.js,则可以使用create-jquery-plugin CLI 实用程序。 Just run赶紧跑

npx create-jquery-plugin

Or, you can clone the jquery-plugin-boilerplate to get started.或者,您可以克隆jquery-plugin-boilerplate以开始使用。

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

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