开发者

According to jQuery my custom function in not a function

开发者 https://www.devze.com 2023-03-12 04:33 出处:网络
Error message (Last line): $(\"select\").selectList is not a function Code: (function( $ ){ $.fn.selectList = function(options) {

Error message (Last line):

$("select").selectList is not a function

Code:

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

    var settings = {
        'buttonClass' : 'custom-select',
        'buttonTextClass' : 'custom-select-status',
        'buttonIconClass' : 'custom-select-button-icon',
        'menuClass' : 'custom-select-menu',
        'menuClassHidden' : 'custom-select-开发者_开发百科menu-hidden'
    }
    $('body').click(function() {
       $('.' + settings.menuClass).each(function() {
           $(this).addClass(settings.menuClassHidden);
       })
    });
    return this.each(function() {
        if (options) {
            $.extend(settings, options);
        }
        var $this = $(this).hide(),
            $menu = $('<ul></ul>').addClass(settings.menuClass)
                                  .addClass(settings.menuClassHidden),
            optionsTexts = new Array(),
            currIdx;

        $this.find('option').each(function(idx) {
            var $opt = $(this);
            if ($opt.is(':selected')) {
                currIdx = idx;
            }
            optionsTexts[idx] = $opt.text();
        })
        for (var i = 0; i < optionsTexts.length; i++) {
            var $item = $('<li></li>'),
            $link = $('<a href="#">' + optionsTexts[i] + '</a>');
            if (i == currIdx) {
                $item.addClass('selected');
            }
            $link.click(function() {
               var linkIdx = $link.parent().parent().find('li').index($link.parent());
                $this.find('option').eq(linkIdx).attr('selected', 'selected');
                $menu.prev().find('.' + settings.buttonTextClass).text($link.text());
                if ($menu.hasClass(settings.menuClassHidden)) {
                    $menu.removeClass(settings.menuClassHidden);
                } else {
                    $menu.addClass(settings.menuClassHidden); 
                }
            });
            $item.append($link); 
            $menu.append($item); 
        }
        $menu.insertBefore($this);
        $('<a href="#"></a>')
        .html('<span class="'+ settings.buttonTextClass + '">'+ optionsTexts[currIdx] +'</span><span class="' + settings.buttonIconClass + '"></span>')
        .addClass(settings.buttonClass)
        .insertBefore($menu)
        .click(function() {
            $('.custom-select-menu').addClass(settings.menuClassHidden);
            $menu.hasClass(settings.menuClassHidden) 
                ? $menu.removeClass(settings.menuClassHidden)
                : $menu.addClass(settings.menuClassHidden);
            return false;
        });
    });
  };
})(jQuery);



$(document).ready(function() {
    if (! $('.section-admin').length > 0) {
        $('select').selectList();
    }
});

Could somebody help?


This works perfect.

See an working example here: Custom jQuery works


Please check if somewhere, somehow you're not initializing jQuery twice. See this answer: https://stackoverflow.com/a/33054683/1712145

0

精彩评论

暂无评论...
验证码 换一张
取 消