简体   繁体   中英

Issues with my jQuery plugin

I have created a basic plugin for accordion. My code is in my accordion.js file, which I have included right after including jQuery library. There is no problem with the accordion with this code in accordion.js file.

(function($) {
$.fn.accordion = function(options) {
    var settings = $.extend({
        speed:300,
    }, options);
    return this.each(function() {
        var $elm = $(this),
            s = settings.speed;
        $elm.on('click',function(e) {
            $('.accordion-toggle').on('click', function(e){
                $elm.each(function() {
                    if(!$(e.target).is(this)) {
                        $($elm.attr('data-target')).slideUp(s); 
                        $(this).removeClass('active');
                    }
                });
            });
            $($elm.attr('data-target')).slideToggle(s);
            $elm.toggleClass('active');
            e.stopPropagation();
        });
    });
}}(jQuery)); $(".accordion-toggle").accordion();

But when I initiate this with speed option from my page, it occurs twice.

$(".accordion-toggle").accordion({ speed: 500, });

Where is the problem?

In your accordion.js file you have $(".accordion-toggle").accordion(); can you remove that and try.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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