How to insert close button in popover for bootstrap

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

JS:

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


        
相关标签:
25条回答
  • 2020-11-29 21:38

    For me this is the simplest solution to add a close button in a popover.

    HTML:

        <button type="button" id="popover" class="btn btn-primary" data-toggle="popover" title="POpover" data-html="true">
                        Show popover
        </button>
    
        <div id="popover-content" style="display:none"> 
           <!--Your content-->
           <button type="submit" class="btn btn-outline-primary" id="create_btn">Create</button>
           <button type="button" class="btn btn-outline-primary" id="close_popover">Cancel</button>  
        </div>
    

    Javascript:

        document.addEventListener("click",function(e){
            // Close the popover 
            if (e.target.id == "close_popover"){
                    $("[data-toggle=popover]").popover('hide');
                }
        });
    
    0 讨论(0)
  • 2020-11-29 21:39

    Previous examples have two main drawbacks:

    1. The 'close' button needs in some way, to be aware of the ID of the referenced-element.
    2. The need of adding on the 'shown.bs.popover' event, a 'click' listener to the close button; which is also not a good solution because of, you would then be adding such listener each time the 'popover' is shown.

    Below is a solution which has not such drawbacks.

    By the default, the 'popover' element is inserted immediately after the referenced-element in the DOM (then notice the referenced-element and the popover are immediate sibling elements). Thus, when the 'close' button is clicked, you can simply look for its closest 'div.popover' parent, and then look for the immediately preceding sibling element of such parent.

    Just add the following code in the 'onclick' handler of the 'close button:

    $(this).closest('div.popover').popover('hide');
    

    Example:

    var genericCloseBtnHtml = '<button onclick="$(this).closest(\'div.popover\').popover(\'hide\');" type="button" class="close" aria-hidden="true">&times;</button>';
    
    $loginForm.popover({
        placement: 'auto left',
        trigger: 'manual',
        html: true,
        title: 'Alert' + genericCloseBtnHtml,
        content: 'invalid email and/or password'
    });
    
    0 讨论(0)
  • 2020-11-29 21:39
    $popover = $el.popover({
      html: true
      placement: 'left'
      content: 'Do you want to a <b>review</b>? <a href="#" onclick="">Yes</a> <a href="#">No</a>'
      trigger: 'manual'
      container: $container // to contain the popup code
    });
    
    $popover.on('shown', function() {
      $container.find('.popover-content a').click( function() {
        $popover.popover('destroy')
      });
    });
    
    $popover.popover('show')'
    
    0 讨论(0)
  • 2020-11-29 21:40

    I've checked many of the mentioned code examples and for me the approach from Chris is working perfectly. I've added my code here to have a working demo of it.

    I have tested it with Bootstrap 3.3.1 and I haven't tested it with an older version. But it's definitely not working with 2.x because the shown.bs.popover event was introduced with 3.x.

    $(function() {
      
      var createPopover = function (item, title) {
                           
            var $pop = $(item);
            
            $pop.popover({
                placement: 'right',
                title: ( title || '&nbsp;' ) + ' <a class="close" href="#">&times;</a>',
                trigger: 'click',
                html: true,
                content: function () {
                    return $('#popup-content').html();
                }
            }).on('shown.bs.popover', function(e) {
                //console.log('shown triggered');
                // 'aria-describedby' is the id of the current popover
                var current_popover = '#' + $(e.target).attr('aria-describedby');
                var $cur_pop = $(current_popover);
              
                $cur_pop.find('.close').click(function(){
                    //console.log('close triggered');
                    $pop.popover('hide');
                });
              
                $cur_pop.find('.OK').click(function(){
                    //console.log('OK triggered');
                    $pop.popover('hide');
                });
            });
    
            return $pop;
        };
    
      // create popover
      createPopover('#showPopover', 'Demo popover!');
    
      
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
    
    <button class="btn btn-primary edit" data-html="true" data-toggle="popover" id="showPopover">Show</button>
    
    <div id="popup-content" class="hide">
        <p>Simple popover with a close button.</p>
        <button class="btn btn-primary OK">OK</button>
    </div>

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

    enter image description here

    The following is what i just used in my project .And hope it can help you

    <a id="manualinputlabel" href="#" data-toggle="popover" title="weclome to use the sql quer" data-html=true  data-original-title="weclome to use the sql query" data-content="content">example</a>
    
    
    $('#manualinputlabel').click(function(e) {
                  $('.popover-title').append('<button id="popovercloseid" type="button" class="close">&times;</button>');
                  $(this).popover();
    
              });
    
          $(document).click(function(e) {
             if(e.target.id=="popovercloseid" )
             {
                  $('#manualinputlabel').popover('hide');                
             }
    
          });
    
    0 讨论(0)
  • 2020-11-29 21:41
        $('.tree span').each(function () {
            var $popOverThis = $(this);
            $popOverThis.popover({
                trigger: 'click',
                container: 'body',
                placement: 'right',
                title: $popOverThis.html() + '<button type="button" id="close" class="close" ">&times;</button>',
                html: true,
                content: $popOverThis.parent().children("div.chmurka").eq(0).html()
            }).on('shown.bs.popover', function (e) {
                var $element = $(this);
                $("#close").click(function () {
                    $element.trigger("click");
                });
            });
        });
    

    When you click element and show your popup, next you can raise event shown.bs.popover where in this you get element in which trigger click to close your popover.

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