Dynamically add a class to Bootstrap's 'popover' container

后端 未结 22 2533
名媛妹妹
名媛妹妹 2020-11-29 18:27

I\'ve thoroughly searched through both StackOverflow and Google, but come up empty. So apologies in advance if this has been asked & resolved already.

NB

22条回答
  •  情歌与酒
    2020-11-29 18:41

    The best solution for this problem is to extend the popover and built your own version of Popover. Below is the code and it's based on bootstrap version 3.3.7

            (function($){
                var PopoverEx = function(element, options){
                    this.init('popover', element, options);
                }
    
                    PopoverEx.prototype = $.extend({}, $.fn.popover.Constructor.prototype, {
    
                        constructor: PopoverEx,
                        tip: function(){
                            if(!this.$tip){
                                this.$tip = $(this.options.template);
                                if(this.options.modifier) this.$tip.addClass(this.options.modifier);
                            }
                            return this.$tip; 
                        }       
                    });
    
                $.fn.popoverex = function(option){
                   return this.each(function () {
                    var $this   = $(this)
                    var data    = $this.data('bs.popover')
                    var options = typeof option == 'object' && option
    
                    if (!data && /destroy|hide/.test(option)) return
                    if (!data) $this.data('bs.popover', (data = new PopoverEx(this, options)))
                    if (typeof option == 'string') data[option]()
                  })
                }
            })(window.jQuery);
    

    Usage

    HTML Code

        Hover for popover   
    

    JS script

        jQuery(document).ready(function($) {
            $('[rel="popover"]').popoverex();
        });
    

    You will find full version and description from this git page https://gist.github.com/vinoddC/475669d94e97f4d7b6bcfde4cef80420

    It's a updated version of Brian Woodward's work.

提交回复
热议问题