How to insert close button in popover for bootstrap

前端 未结 25 2965
忘了有多久
忘了有多久 2020-11-29 20:53

JS:

$(function(){
  $(\"#example\").popover({
    placement: \'bottom\',
    html: \'true\',
    title : \'         


        
25条回答
  •  日久生厌
    2020-11-29 21:15

    I needed to find something that would work for multiple popovers and in Bootstrap 3.

    Here's what I did:

    I had multiple elements I wanted to have a popover work for, so I didn't want to use ids.

    The markup could be:

    
    
    
    

    The content for the save and close buttons inside the popover:

    var contentHtml = [
        '
    ', '', '', '
    '].join('\n');

    and the javascript for all 3 buttons:

    This method works when the container is the default not attached to body.

    $('.foo').popover({
        title: 'Bar',
        html: true,
        content: contentHtml,
        trigger: 'manual'
    }).on('shown.bs.popover', function () {
        var $popup = $(this);
        $(this).next('.popover').find('button.cancel').click(function (e) {
            $popup.popover('hide');
        });
        $(this).next('.popover').find('button.save').click(function (e) {
            $popup.popover('hide');
        });
    });
    

    When the container is attached to 'body'

    Then, use the aria-describedby to find the popup and hide it.

    $('.foo').popover({
        title: 'Bar',
        html: true,
        content: contentHtml,
        container: 'body',
        trigger: 'manual'
    }).on('shown.bs.popover', function (eventShown) {
        var $popup = $('#' + $(eventShown.target).attr('aria-describedby'));
        $popup.find('button.cancel').click(function (e) {
            $popup.popover('hide');
        });
        $popup.find('button.save').click(function (e) {
            $popup.popover('hide');
        });
    });
    

提交回复
热议问题