Can I use complex HTML with Twitter Bootstrap's Tooltip?

后端 未结 5 1079
一生所求
一生所求 2020-11-27 11:13

If I check official documentation, I can see a property called HTML:

Name    |    Type       |    default  |    Description
---------------------------------         


        
相关标签:
5条回答
  • 2020-11-27 11:52

    The html data attribute does exactly what it says it does in the docs. Try this little example, no JavaScript necessary (broken into lines for clarification):

    <span rel="tooltip" 
         data-toggle="tooltip" 
         data-html="true" 
         data-title="<table><tr><td style='color:red;'>complex</td><td>HTML</td></tr></table>"
    >
    hover over me to see HTML
    </span>
    


    JSFiddle demos:

    • Bootstrap 2.x
    • Bootstrap 3.x
    0 讨论(0)
  • 2020-11-27 11:56

    Another solution to avoid inserting html into data-title is to create independant div with tooltip html content, and refer to this div when creating your tooltip :

    <!-- Tooltip link -->
    <p><span class="tip" data-tip="my-tip">Hello world</span></p>
    
    <!-- Tooltip content -->
    <div id="my-tip" class="tip-content hidden">
        <h2>Tip title</h2>
        <p>This is my tip content</p>
    </div>
    
    <script type="text/javascript">
        $(document).ready(function () {
            // Tooltips
            $('.tip').each(function () {
                $(this).tooltip(
                {
                    html: true,
                    title: $('#' + $(this).data('tip')).html()
                });
            });
        });
    </script>
    

    This way you can create complex readable html content, and activate as many tooltips as you want.

    live demo here on codepen

    0 讨论(0)
  • 2020-11-27 11:58

    set "html" option to true if you want to have html into tooltip. Actual html is determined by option "title" (link's title attribute shouldn't be set)

    $('#example1').tooltip({placement: 'bottom', title: '<p class="testtooltip">par</p>', html: true});
    

    Live sample

    0 讨论(0)
  • 2020-11-27 11:59

    Just as normal, using data-original-title:

    Html:

    <div rel='tooltip' data-original-title='<h1>big tooltip</h1>'>Visible text</div>
    

    Javascript:

    $("[rel=tooltip]").tooltip({html:true});
    

    The html parameter specifies how the tooltip text should be turned into DOM elements. By default Html code is escaped in tooltips to prevent XSS attacks. Say you display a username on your site and you show a small bio in a tooltip. If the html code isn't escaped and the user can edit the bio themselves they could inject malicious code.

    0 讨论(0)
  • 2020-11-27 12:00

    This parameter is just about whether you are going to use complex html into the tooltip. Set it to true and then hit the html into the title attribute of the tag.

    See this fiddle here - I've set the html attribute to true through the data-html="true" in the <a> tag and then just added in the html ad hoc as an example.

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