简体   繁体   English

具有工具提示的Bootstrap 3 Popover

[英]Bootstrap 3 popover with tooltip

I have code: 我有代码:

    <div class="container-fluid">
    <div class="row">
        <div class="col-lg-2 col-lg-offset-10">
            <a href="#" style="display: inline-block" class="pop" data-placement="bottom" ><img src="https://media.licdn.com/mpr/mpr/shrinknp_100_100/p/4/005/079/057/3730ae6.png" alt="img"></a>
        </div>
      </div>
    </div>

and some javascript 和一些JavaScript

    <script>
    $(function () {
        $('[data-toggle="tooltip"]').tooltip();
        $('.pop').popover({
            html: true,
            title: '<h4 class="text-primary">Profile</h4>',
            content: '<a data-toggle="tooltip" data-placement="left" title="Preferences" href="#" class="btn btn-primary btn-sm"><span class="glyphicon glyphicon-cog"></span></a> <a href="#" class="btn btn-success btn-sm"><span class="glyphicon glyphicon-user"></span></a>',
            trigger: 'focus'
            });
        });
    </script>

but tooltip doesn't work... I think that is problem because I return dynamically content from js... 但是工具提示不起作用...我认为这是有问题的,因为我从js动态返回内容...

What you need is a callback on the popover creation, because trying to instantiate the tooltip before the DOM element is created will not work... 您需要的是在popover创建上的回调,因为尝试在创建DOM元素之前实例化工具提示将不起作用...

To make a callback function available, extend the popover function first : 要使回调函数可用,请首先扩展popover函数:

var tmp = $.fn.popover.Constructor.prototype.show;
$.fn.popover.Constructor.prototype.show = function() {
    tmp.call(this); if (this.options.callback) {
        this.options.callback();
    }
}

Then, call your tooltip instantiation in the callback : 然后,在回调中调用您的工具提示实例化:

<script>
    $(function () {
        $('.pop').popover({
            html: true,
            title: '<h4 class="text-primary">Profile</h4>',
            content: '<a data-toggle="tooltip" data-placement="left" title="Preferences" href="#" class="btn btn-primary btn-sm"><span class="glyphicon glyphicon-cog"></span></a> <a href="#" class="btn btn-success btn-sm"><span class="glyphicon glyphicon-user"></span></a>',
            trigger: 'focus',
            callback: function () {
               $('a[data-toggle="tooltip"]').tooltip();
            }
        });
    });
</script>

PS : Even if it is not about performance, try to use $('a[data-toggle="tooltip"]').tooltip(); PS:即使与性能无关,请尝试使用$('a[data-toggle="tooltip"]').tooltip(); . It will search all the "a" tags with this attribute instead of the whole DOM... Useful especially if you have a ton of html. 它将使用此属性而不是整个DOM搜索所有的“ a”标签...很有用,尤其是在您拥有大量html的情况下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM