jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jQuery插件创建和面向公众的方法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经创建了一个插件,可以使用DIV将 HTML选择框转换为自定义下拉列表.

一切运作良好,但我想让它好一点. see my jsFiddle

插件的最后我有2个方法,slideDownOptions& slideUpOptions,我想在插件之外使这些可用,以便其他事件可以触发操作.

我有点困惑如何做到这一点,更具体地说,如何从插件内部和插件外部调用方法.

任何帮助总是赞赏

解决方法

虑使用面向对象的代码重构您的插件.有了这个,您可以为您的插件制作API,如jQuery UI API.所以你可以访问插件方法,如:

$('SELEct').customSELEct(); // apply plugin to elements
$('SELEct').customSELEct('resetOpacity'); // call method resetOpacity();
$('SELEct').customSELEct('setOpacity',0.5); // call method with arguments

用于创建此类插件的基本模板如下所示:

// plugin example
(function($){
    // custom SELEct class
    function CustomSELEct(item,options) {
        this.options = $.extend({
            foo: 'bar'
        },options);
        this.item = $(item);
        this.init();
    }
    CustomSELEct.prototype = {
        init: function() {
            this.item.css({opacity:0.5});
        },resetOpacity: function() {
            this.setOpacity('');
        },setOpacity: function(opacity) {
            this.item.css({opacity:opacity});
        }
    }

    // jQuery plugin interface
    $.fn.customSELEct = function(opt) {
        // slice arguments to leave only arguments after function name
        var args = Array.prototype.slice.call(arguments,1);
        return this.each(function() {
            var item = $(this),instance = item.data('CustomSELEct');
            if(!instancE) {
                // create plugin instance and save it in data
                item.data('CustomSELEct',new CustomSELEct(this,opt));
            } else {
                // if instance already created call method
                if(typeof opt === 'String') {
                    instance[opt].apply(instance,args);
                }
            }
        });
    }

}(jQuery));

// plugin tesTing
$('SELEct').customSELEct();

工作JS在这里小提琴:http://jsfiddle.net/XsZ3Z/

大佬总结

以上是大佬教程为你收集整理的jQuery插件创建和面向公众的方法全部内容,希望文章能够帮你解决jQuery插件创建和面向公众的方法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。