Change Twitter Bootstrap Tooltip content on click

前端 未结 25 1380
情深已故
情深已故 2020-11-27 10:02

I have a tooltip on an anchor element, that sends an AJAX request on click. This element has a tooltip (from Twitter Bootstrap). I want the tooltip content to change when th

25条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 10:26

    Change the text by altering the text in the element directly. (does not update the tooltip position).

    $('.tooltip-inner', $element.next()).html(newHtml);
    $('.tooltip-inner', $element.next()).text(newText);
    

    Change the text by destroying the old tooltip, then creating and showing a new one. (Causes the old one to fade out and the new one to fade in)

    $element
    .tooltip('destroy')
    .tooltip({
        // Repeat previous options.
        title: newText,
    })
    .tooltip('show');
    

    I'm using the top method to both animate the "Saving." message (using   so the tooltip does not change in size) and to change the text to "Done." (plus padding) when the request completes.

    $element.tooltip({
      placement: 'left',
      title: 'Saving...',
      trigger: 'manual',
    }).tooltip('show');
    
    var parent = $element.parent();
    var interval_id = setInterval(function(){
        var text = $('.tooltip-inner', parent).html();
        switch(text) {
        case 'Saving.  ': text = 'Saving.. '; break;
        case 'Saving.. ': text = 'Saving...'; break;
        case 'Saving...': text = 'Saving.  '; break;
        }
        $('.tooltip-inner', parent).html(text);
    }, 250);
    
    send_request( function(){
        // When the request is complete
        clearInterval(interval_id);
        $('.tooltip-inner', parent).html('Done.    ');
        setTimeout(function() {
            $element.tooltip('hide');
        }, 1500 /* Show "Done." for a bit */);
    });
    

提交回复
热议问题