【问题标题】:Why doesn't the style of the bootstrap tooltip show?为什么引导工具提示的样式不显示?
【发布时间】:2019-04-21 04:37:39
【问题描述】:

我正在尝试在引导工具提示中设置文本样式。我的要求很简单:我希望第一个按钮上的工具提示文本显示为红色。我用

data-html="true"

在我的工具提示中使用 HTML。为什么不是红色的?

<button data-toggle="tooltip" data-html="true" data-trigger="click" title="<div style='color:red;'>this text should be red</div>">
    Press me
</button>

同样,我希望首先显示以下工具提示中的工具提示。 table 环境完全隐藏了它。这是同样的问题吗?

<button data-toggle="tooltip" data-html="true" data-trigger="click" title="<table><tr><td>this text should appear</td></tr></table>">
    Press me 2
</button>

我创建了一个可以清楚地显示问题的小提琴。

https://jsfiddle.net/ws9z37q2/9/

任何帮助将不胜感激。提前致谢!

【问题讨论】:

    标签: javascript jquery css bootstrap-4 tooltip


    【解决方案1】:

    给元素一个类,并将样式应用到 CSS 中的类。

    $(function() {
      $('[data-toggle="tooltip"]').tooltip()
    })
    .red {
      color: red
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
    
    
    <button data-toggle="tooltip" data-html="true" data-trigger="click" title="<div class='red'>this text should be red</div>">
      Press me
    </button>

    当我在 Github 中检查 Bootstrap 的源代码时,默认情况下它是 sanitizing the HTML contentstyle 属性是不允许的,check here for more info

    因此,您也可以通过禁用 sanitize 选项(不推荐)使其正常工作。

    $(function() {
      $('[data-toggle="tooltip"]').tooltip({
        sanitize: false
      })
    })
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
    
    
    <button data-toggle="tooltip" data-html="true" data-trigger="click" title="<div style='color:red'>this text should be red</div>">
      Press me
    </button>

    【讨论】:

    • @L.Scheuvens :很高兴它有帮助 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    • 2013-11-26
    相关资源
    最近更新 更多