Bootstrap popover content cannot changed dynamically

后端 未结 14 2206
旧时难觅i
旧时难觅i 2020-11-29 22:40

I use the code as follows:

$(\".reply\").popover({
  content: \"Loading...\",
  placement: \"bottom\"
});

$(\".reply\").popover(\"toggle\");
相关标签:
14条回答
  • 2020-11-29 23:12

    After the popover has been correctly initialized, you can directly use jquery to replace the class="popover-content" element:

    $(".popover-content").html('a nice new content')
    
    0 讨论(0)
  • 2020-11-29 23:15

    Yes you can, in fact the easiest way haven't been suggested yet.

    Here's my way ->

        var popover = $('#example').data('bs.popover');
        popover.options.content = "YOUR NEW TEXT";
    

    popover is an object if you want to know more about it, try to do console.log(popover) after you define it to see how you can use it after !

    EDIT

    As of Bootstrap 4 alpha 5, the data structure is a bit different. You'll need to use popover.config.content instead of popover.options.content

    Thanks to @kfriend for the comment

    0 讨论(0)
  • 2020-11-29 23:22
    <button data-toggle="popover" data-html="true" data-content='<div id="myPopover">content</div>'>click</button>
    $('#myPopover').html('newContent')
    

    This is a very clean way.

    0 讨论(0)
  • 2020-11-29 23:23

    I solved the problem using @David and @Sujay sreedhar's answer, but if the popover is visible during the new content is loaded, the popover needs to be repositioned:

    var popover = thisVal.attr('data-content',data).data('popover');
    popover.setContent();
    popover.$tip.addClass(popover.options.placement);
    // if popover is visible before content has loaded
    if ($(".reply").next('div.popover:visible').length){
        // reposition
        popover.show();
    }
    

    If it is not repositioned and the new content has e.g. a different height, the popover will expand downwards and the arrow will be off target!

    Also, when the button is clicked afterwards it will re-open the popover instead of closing it. The above code solves, that problem too.

    Demo http://jsfiddle.net/whFv6/

    (My edit was rejected, so I thought I'd post it here..)

    0 讨论(0)
  • 2020-11-29 23:23

    HTML

    <a data-toggle="popover" popover-trigger="outsideClick" title="Indicadores" data-content="" data-html="true" class="Evaluar" id="alun codigo"><span><i class="fa fa-check"></i>algun nombre</span></a>
    

    JQUERY

    $('a.Evaluar').on('click', function () {
        var a=$(this);//.attr('data-content', "mañana");
        $.ajax({
            url: "../IndicadorControlador?accion=BuscarPorProceso&cP=24",
            type: 'POST',
            dataType: 'json'
        })
                .done(function (response) {
                    //alert("done. ");
                    var ind = "";
                    $(response).each(function (indice, elemento) {
                        //alert(elemento.strIdentificador);
                        //console.log('El elemento con el índice ' + indice + ' contiene ' + elemento.strIdentificador.toString());
                        ind += '<a href=#>'+elemento.strIdentificador.toString()+'</a>';
                    });
                  $(a).attr('data-content', ind);
                })
                .fail(function () {
                    sweetAlert("ERROR!", " Algo salió mal en el controlador!", "error");
                })
                .complete(function () {
                });
    
        $('[data-toggle="popover"]').popover();
    });
    
    0 讨论(0)
  • 2020-11-29 23:23

    For Boostrap v4.3

    The following code update the popover content just before it is displayed:

    // When the popover is show
    $('.my-popover').on('show.bs.popover', function () {
      // Update popover content
      $(this).attr('data-content', 'New content');
    })
    

    Test online on this JSFiddle.

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