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

后端 未结 22 2455
名媛妹妹
名媛妹妹 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:40

    Here is my workaround, probably not the most efficient but I found it the easiest to implement.

     $('[data-content]').each(function(){
        var options = {
            html: true //optional
        };
    
        if ($(this)[0].hasAttribute('data-class')) {
            options['template'] = '<div class="popover" role="tooltip ' + $(this).attr('data-class') + '"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>';
        }
    
        $(this).popover(options);
    });
    

    Just add data-class="custom-class" to the element like the other examples.

    0 讨论(0)
  • 2020-11-29 18:41

    This extend your class from out side bootstrap core class, just add the attribute data-class and an option dataClass: true to your tooltip function

    !function($){   
        $.extend($.fn.tooltip.Constructor.DEFAULTS,{
            dataClass: false
        }); 
        var Tooltip = $.fn.tooltip.Constructor;
            _show = Tooltip.prototype.show;
    
        Tooltip.prototype.show = function (){
            _show.apply(this,Array.prototype.slice.apply(arguments));
    
            if (this.options.dataClass!=="undefined" && this.options.dataClass){
                var that = this;
                var $tip = this.tip();
                if (this.$element.attr("data-class") !== undefined)
                    $tip.addClass(this.$element.attr("data-class"));
            }
        };
    }(jQuery);
    
    0 讨论(0)
  • 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

        <a href="#" class="btn btn-large btn-danger" 
              rel="popover" 
              data-modifier="popover-info" 
              title="A Title" 
              data-content="And here's some amazing content.right?">Hover for popover</a>   
    

    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.

    0 讨论(0)
  • 2020-11-29 18:43

    Sorry, but did not quite understand your question ... But what you want is to add a parent div? Take it easy ... See if this is what you want:

    $(function(){
        $('a[rel=popover]')
        .popover({
            placement : 'bottom',
            trigger : 'hover'
        })
        .on("click", function(){
            $(this).closest("div").addClass($(this).data("class")); //Add class .dynamic-class to <div>
        });
    });
    

    Demo: http://jsfiddle.net/PRQfJ/

    0 讨论(0)
  • 2020-11-29 18:44

    Just set the hidden "template" option when initializing the tooltip. I don't know why the bootstrap team would keep this a secret...

    $(elem).popover({
        template: '<div class="popover YOURCLASS"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>'
    }).popover('show');
    

    Hope this helps...

    0 讨论(0)
  • 2020-11-29 18:45

    Its an old post but I'm adding it just as reference. Modifying Shankar Cabus answer, instead of adding the dynamic-class to parent, it will be added in the created .popover div.

    $(function(){
        $('a[rel=popover]')
        .popover({
            placement : 'bottom',
            trigger : 'hover'
        })
        .on("hover", function(){
            $('.popover').addClass($(this).data("class")); //Add class .dynamic-class to <div>
        });
    });
    

    Hope this helps :)

    0 讨论(0)
提交回复
热议问题