【问题标题】:Formatting text in text-hover-box格式化文本悬停框中的文本
【发布时间】:2015-06-28 03:28:25
【问题描述】:

我正在使用一些代码,当您将鼠标悬停在某些文本上时会显示一个文本框。问题是文本框中显示的文本来自它所在的<a> 标记的属性,我无法对其进行格式化。我想在文本框中做一个列表,但我只能让它只是一堵文字墙。

HTML:

<a data-text="Could use this text instead of title" title="1. ~~~~ List item (/br)~~~~ 
                                    2. ~~~~ List item ~~~~ 
                                    3. ~~~~ List item ~~~~ 
                                    4. ~~~~ List item ~~~~ 
                                    5. ~~~~ List item ~~~~ " class="tooltip">Hover over this text</a>   

CSS:

.tooltip{
    display: inline;
    position: relative;
}

.tooltip:hover:after{
    background: #333;
    background: rgba(0,0,0,.8);
    border-radius: 5px;
    bottom: 26px;
    color: #fff;
    content: attr(title);
    left: 100%;
    padding: 5px 15px;
    position: absolute;
    z-index: 98;
    width: 220px;
}

content: attr(title);

.tooltip:hover:before{
    border: solid;
    border-color: #333 transparent;
    border-width: 6px 6px 0 6px;
    bottom: 20px;
    content: "";
    left: 50%;
    position: absolute;
    z-index: 99;
}

查看此演示:http://jsfiddle.net/h3tqwust/1/

如何为此设置文本格式(有换行符)?

【问题讨论】:

  • 你能给我们创建一个demo吗?
  • @divy3993 我添加了一个演示
  • 是的,我看到了,就你所需要的而言,@Marc Audet 有正确的答案。
  • 如果您仍然需要使用 JS/JQuery 的完全响应式工具提示,并且任何格式都没有问题,请随时询问。

标签: html css


【解决方案1】:

这是一种简单的格式化方法,使用white-space: pre 保留:after 伪元素中的空白,并确保title 属性的格式正确。

但是,由于插入内容的解析方式,&lt;b&gt; 等 HTML 标记将不起作用。

如果您需要更复杂的样式,那么您需要 jQuery/JavaScript 辅助解决方案。

.tooltip{
    display: inline;
    position: relative;
}

.tooltip:hover:after{
    background: #333;
    background: rgba(0,0,0,.8);
    border-radius: 0px 5px 5px 5px;
    top: 15px;
    color: #fff;
    content: attr(title);
    left: 100%;
    padding: 5px 15px;
    position: absolute;
    z-index: 98;
    width: 220px;
    white-space: pre;
}
<a
title="1. ~~~~ List item ~~~~ 
2. ~~~~ List item ~~~~ 
3. ~~~~ List item ~~~~ 
4. ~~~~ List item ~~~~ 
5. ~~~~ List item ~~~~" 
   class="tooltip">Hover over this text</a>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-02
    • 2011-05-03
    • 2011-08-21
    • 2018-06-25
    • 1970-01-01
    • 2019-03-04
    • 2014-01-20
    相关资源
    最近更新 更多