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

后端 未结 22 2438
名媛妹妹
名媛妹妹 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 19:05

    In Bootstrap 4 you can use the data-template attribute:

    <button data-toggle="popover" data-template='<div class="popover MYSUPERCLASS" role="tooltip"><div class="popover-arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>' data-offset="-10 0" data-html="true" data-trigger="focus" data-placement="bottom" data-content='My Popover'>Button</button>
    
    0 讨论(0)
  • 2020-11-29 19:06

    I know this thread is old, but for those who stumble across this article as I did, here is another method that will allow more customisation. I haven't tested it with Bootstrap 4 but it with Bootstrap 3.

    Instead of hard-coding a class in the function, you can 'submit' a css class to your popover via your html element using a custom data attribute. For this example I've called that attribute "data-class".

    As this method exploits the function available to the popovers 'placement' options, I've configured it to preserve the original placement configured in the popover.

    jQuery( '[data-toggle="popover"]' ).popover({
    
        container: 'body',
    
        placement: function ( popover, trigger ){
    
            // Get the submitted placement for the popover
            var placement = jQuery( trigger ).attr( 'data-placement' );
    
            // Get the class(es) to apply to the popover
            var dataClass = jQuery( trigger ).attr( 'data-class' );
    
            // Apply the submitted class(es) to the popover element
            jQuery( popover).addClass( dataClass );
    
            // Return the original placement for the popover
            return placement;
    
            },
    
            trigger: 'hover'
    
    });
    

    I hope this helps. If anyone has a better or cleaner way of doing this I'd be happy to learn :)

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

    You can do this without hacking Bootstrap and without changing the template either, by grabbing the popover object from the caller's data and accessing its $tip property.

    $('a[rel=popover]')
      .popover({ placement: 'bottom', trigger: 'hover' })
      .data('bs.popover')
      .tip()
      .addClass('my-super-popover');
    
    0 讨论(0)
  • 2020-11-29 19:07

    It's getting late over here and I'm getting tired but here's a quick one-liner that won't work in the future if you decide to update bootstrap's js files.

    Take a look at the bootstrap-tooltip.js file in this gist on line 150.

    And here's the modified tooltip in action:

    Here's the modified tooltip in action

    Checkout the inspector's window down there and you'll notice that the dynamic-class has been added to the tooltip.

    I'll post a more long-termable & appropriate answer tomorrow.

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