如何在HTML工具提示中添加换行符?

我尝试在工具提示中使用<br/>和\n,如下所示:

<a href="#" title="Some long text <br/> Second line text \n Third line text">Hover me</a>

然而,这是无用的,我可以在工具提示中看到文字<br/>和\n。任何建议都会很有帮助。


当前回答

如果你使用jquery-ui 1.11.4:

var tooltip = $.widget( "ui.tooltip", {
    version: "1.11.4",
    options: {
        content: function() {
            // support: IE<9, Opera in jQuery <1.7
            // .text() can't accept undefined, so coerce to a string
            var title = $( this ).attr( "title" ) || "";
            // Escape title, since we're going from an attribute to raw HTML
Replace-->  //return $( "<a>" ).text( title ).html();
by -->      return $( "<a>" ).html( title );
             },

其他回答

这个css会帮助你。

    .tooltip {
      word-break: break-all;
    }

or if you want in one line

    .tooltip-inner {
      max-width: 100%;
    }

在文本之间给出\n。它适用于所有浏览器。

Example 
img.tooltip= " Your Text : \n"
img.tooltip += " Your text \n";

这将为我工作,它在后面的代码中使用。

希望这对你有用

嗨,这段代码将在所有浏览器中工作!!我使用 用于chrome和safari的新产品线和IE的ul li

 function genarateMultiLIneCode(){
        var =values["a","b","c"];
        const liStart = '<li>';
              const liEnd = '</li>';
              const bullet = '&#8226; ';     
              var mergedString = ' ';
              const unOrderListStart='<ul>'
              const unOrderListEnd='</ul>'
              const fakeNewline = '&#013;&#010;';
              for (let i = 0; i < values.length; i++) {
                   mergedString += liStart + bullet + values[i] + liEnd + fakeNewline;
              }
              const tempElement = document.createElement("div");
              tempElement.innerHTML = unOrderListStart + mergedString + unOrderListEnd;    
              return tempElement.innerText;
            }
        }

如果你用的是bootstrap4,那么这个是可行的。

<style>
   .tooltip-inner {
    white-space: pre-wrap;
   }

</style>

<script> 
    $(function () {
      $('[data-toggle="tooltip"]').tooltip()
    })
</script>

<a data-toggle="tooltip" data-placement="auto" title=" first line &#010; next line" href= ""> Hover me </a>

如果你在Django项目中使用,那么我们也可以在工具提示中显示动态数据,比如:

<a class="black-text pb-2 pt-1" data-toggle="tooltip" data-placement="auto"  title="{{ post.location }} &#010; {{ post.updated_on }}" href= "{% url 'blog:get_user_profile' post.author.id %}">{{ post.author }}</a>

我找到了。只要这样简单地做就可以了

<a ref="#" title="First Line
                  Second Line
                  Third line">Hover Me</a>