Bootstrap tooltips not working

前端 未结 25 1747
孤城傲影
孤城傲影 2020-11-28 17:46

I\'m going mad here.

I\'ve got the following HTML:



        
25条回答
  •  旧巷少年郎
    2020-11-28 18:26

    If you do $("[rel='tooltip']").tooltip(); as other answers have suggested then you will activate tooltips only on elements that are currently there in DOM. That means if you are going to change DOM and insert dynamic content later it won't work. Also this is much less efficient because it installs event handler for individual elements as opposed to using JQuery event delegation. So the best way I've found to activate Bootstrap tooltips is this one line of code that you can place in document ready and forget about it:

    $(document.body).tooltip({ selector: "[title]" });
    

    Note that I'm using title as selector instead of rel=title or data-title. This has an advantage that it can be applied to many other elements (the rel is supposed to be only for anchors) and also it works as "fallback" for old browsers.

    Also note that you don't need data-toggle="tooltip" attribute if you are using above code.

    Bootstrap tooltips are expensive because you need to handle mouse events on each of the elements. This is the reason why they haven't enabled it by default. So if you ever decide to comment out above line, your page would still work if you use title attribute.

    If you are OK not to use title attribute then I would recommend using pure CSS solution such as Hint.css or check http://csstooltip.com which does not require any JavaScript code at all.

提交回复
热议问题