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

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

    There is another way to do this in version 2.3 that is quite simple actually. You override the default template to include the class to the container.

    var pop = $('a', this.el).popover({
      trigger: 'click'
      , template: '<div class="popover awesome-popover-class"><div class="arrow"></div><div class="popover-inner"><h3 class="popover-title"></h3><div class="popover-content"><p></p></div></div></div>'
    });
    
    0 讨论(0)
  • 2020-11-29 18:58

    I'm not sure why you want to do this, but in my example, I just wanted to set custom style... So in CSS just written right selector for current popover. a.rating-popover - this is my link for opening popover. -> the popover element will be generated to the next of that element. so we can select it with

    a.rating-popover + div.popover{
       background: blue;
    }
    

    and voila, blue background. for only popovers opened with a.rating-popover element.

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

    How about adding that class ONLY to appropriate popover, without targeting others?

    $('#someElement').popover({placement: function(context, src) {
        $(context).addClass('my-custom-class');
        return 'top'; // - 'top' placement in my case
    });
    

    or some variation, like taking custom class name from data of 'someElement', like so:

    $('#someElement').popover({placement: function(context, src) {
        $(context).addClass($(src).data('customClassName'));
        return 'top';
    });
    
    0 讨论(0)
  • 2020-11-29 19:02
    $('a[rel=popover]')
      .popover({ placement: 'bottom', trigger: 'hover' })
      .data('bs.popover')
      .addAttachmentClass('my-own-popover')
    

    You'll get bs-popover-my-own-popover class inside .popover element.

    0 讨论(0)
  • 2020-11-29 19:03

    Also you can make use of 'template' options

    $element.popover({                               
       html: true,
       trigger: 'click',
       template: '<div class="popover '+MY_CLASS+'" role="tooltip"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>',
       content: function() {
        return 'hello';
       }
    });
    

    Update MY_CLASS from you data-class attribute.

    0 讨论(0)
  • 2020-11-29 19:04

    This has been asked a few years ago, and there are plenty of answers. But... I recently had to tackle the same problem myself, and I - (a) wanted to avoid manipulating source code and (b) needed a generic solution to be reused constantly (so using the template: '...' solution for each initialization was out).

    My solution was simple enough, and is sort of the same as the marked answer - I figured popover is an extension of the tooltip.js library. I mean - check it out, the source code is barely more than a hundred lines. So I created a file called popover-extend.js, and copy-pasted the entire popover source code in. From there it was easy - simple manipulate these lines:

    Popover.DEFAULTS = $.extend({}, $.fn.tooltip.Constructor.DEFAULTS, {
        // add this:
        cls: ''
    });
    

    then:

    Popover.prototype.setContent = function () {
        // add this:
        if (this.options.cls) {
            $tip.addClass(this.options.cls);    
        }
    

    Now you can do:

    <a href="#" rel="popover" 
       data-cls="dynamic-class" 
       title="Title goes here" data-content="Content goes here">
    

    It's a really good approach if you're like me and want to add more functionality. for example, here's how I added a generic close button to the title (though it requires the popover to have a specified id):

    // added this to the the DEFAULTS
    close: ''
    
    
    // added this to the setContent function
    if (this.options.close) {
        var id = this.$element.attr('id'),
            btn = $("<button></button>", {
                "class": "close",
                "id": "close",
                "onclick": "$('#"+id+"').popover('hide');"
        }),
    
        title = $tip.find('.popover-title');
    
        btn.html("&times;");
        btn.appendTo(title);
    }
    

    The cool thing about it, is that everything you set in the DEFAULTS can be configured via html, i.e. if you add a variable named foo, you will be automatically able to manipulate it through data-foo=.

    Hope this helps anyone looking for an alternative to manipulating source code

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