How to insert close button in popover for bootstrap

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

JS:

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


        
25条回答
  •  借酒劲吻你
    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 = '';
    
    $loginForm.popover({
        placement: 'auto left',
        trigger: 'manual',
        html: true,
        title: 'Alert' + genericCloseBtnHtml,
        content: 'invalid email and/or password'
    });
    

提交回复
热议问题