How to insert close button in popover for bootstrap

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

JS:

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


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

    FWIW, here's the generic solution that I'm using. I'm using Bootstrap 3, but I think the general approach should work with Bootstrap 2 as well.

    The solution enables popovers and adds a 'close' button for all popovers identified by the 'rel="popover"' tag using a generic block of JS code. Other than the (standard) requirement that there be a rel="popover" tag, you can put an arbitrary number of popovers on the page, and you don't need to know their IDs -- in fact they don't need IDs at all. You do need to use the 'data-title' HTML tag format to set the title attribute of your popovers, and have data-html set to "true".

    The trick that I found necessary was to build an indexed map of references to the popover objects ("po_map"). Then I can add an 'onclick' handler via HTML that references the popover object via the index that JQuery gives me for it ("p_list['+index+'].popover(\'toggle\')"). That way I don't need to worry about the ids of the popover objects, since I have a map of references to the objects themselves with a JQuery-provided unique index.

    Here's the javascript:

    var po_map = new Object();
    function enablePopovers() {
      $("[rel='popover']").each(function(index) {
        var po=$(this);
        po_map[index]=po;
        po.attr("data-title",po.attr("data-title")+
        '<button id="popovercloseid" title="close" type="button" class="close" onclick="po_map['+index+'].popover(\'toggle\')">&times;</button>');
        po.popover({});
      });
    }
    $(document).ready(function() { enablePopovers() });
    

    this solution let me easily put a close button on all the popovers all across my site.

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