How to bind bootstrap tooltip on dynamic elements

前端 未结 2 1053
不思量自难忘°
不思量自难忘° 2020-12-29 03:34

When using Bootstrap tooltip, we need to write something like this to have tooltip functionality:

$(\".showtooltip\").tooltip();

But the pr

相关标签:
2条回答
  • 2020-12-29 04:02

    You need to use selector property. See on the documentation :

    "If a selector is provided, tooltip objects will be delegated to the specified targets. In practice, this is used to enable dynamic HTML content to have tooltips added. See this and an informative example."

    JS example code :

    $('body').tooltip({
        selector: '.createdDiv'
    });
    
    $('#add-button').click(function() {
        $('<div class="createdDiv" data-toggle="tooltip" title="Some tooltip text!">Hover over me</div>').appendTo('#container');
    });
    

    DEMO

    0 讨论(0)
  • 2020-12-29 04:06

    Try this:

    $('body').tooltip({
        selector: '[data-toggle="tooltip"]'
    });
    
    0 讨论(0)
提交回复
热议问题