如何在HTML工具提示中添加换行符?
我尝试在工具提示中使用<br/>和\n,如下所示:
<a href="#" title="Some long text <br/> Second line text \n Third line text">Hover me</a>
然而,这是无用的,我可以在工具提示中看到文字<br/>和\n。任何建议都会很有帮助。
如何在HTML工具提示中添加换行符?
我尝试在工具提示中使用<br/>和\n,如下所示:
<a href="#" title="Some long text <br/> Second line text \n Third line text">Hover me</a>
然而,这是无用的,我可以在工具提示中看到文字<br/>和\n。任何建议都会很有帮助。
在原生HTML工具提示中添加换行符是可能的,只需将标题属性分布在多行中即可。
但是,我建议使用jQuery工具提示插件,如Q-Tip: http://craigsworks.com/projects/qtip/。
它的设置和使用都很简单。另外,也有很多免费的javascript工具提示插件。
编辑:第一个语句的更正。
我找到了。只要这样简单地做就可以了
<a ref="#" title="First Line
Second Line
Third line">Hover Me</a>
如果你使用的是Jquery Tooltip工具,那么在“Jquery -ui.js”Javascript文件中找到以下文本:
tooltip.find(".ui-tooltip-content").html(content);
放在上面
content=content.replace(/\</g,'<').replace(/\>/g,'>');
我希望这对你也有用。
对我来说,两步解决方案(结合格式化标题和添加样式)是有效的,如下所示:
1) title属性的格式:
<a ref="#" title="First Line
Second Line
Third line">Hover Me</a>
2)在tips元素中添加如下样式:
white-space: pre-line;
在IE8、Firefox 22和Safari 5.1.7中测试。
我的解决方案是http://jqueryui.com/tooltip/:
这里的代码(脚本中使<br/>工作的部分是"content:"):
HTML头
<head runat="server">
<script src="../Scripts/jquery-2.0.3.min.js"></script>
<link href="Content/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="../Scripts/jquery-ui-1.10.3.min.js"></script>
<script>
/*Position:
my => at
at => element*/
$(function () {
$(document).tooltip({
content: function() {
var element = $( this );
if ( element.is( "[title]" ) ) {
return element.attr( "title" );
}
},
position: { my: "left bottom-3", at: "center top" } });
});
</script>
</head>
ASPX或HTML控件
<asp:TextBox ID="Establecimiento" runat="server" Font-Size="1.3em" placeholder="Establecimiento" title="Texto 1.<br/>TIP: texto 2"></asp:TextBox>
希望这对大家有所帮助
这个CSS是我在编辑器中与换行一起工作的最终结果:
.tooltip-inner {
white-space: pre-wrap;
}
在这里找到: 如何使Twitter引导工具提示有多行?
Jquery UI 1.10不支持在标题属性中使用html标签,因为它不是有效的html。
所以另一种解决方案是使用工具提示内容选项。 参考- http://api.jqueryui.com/tooltip/#option-content
在文本之间给出\n。它适用于所有浏览器。
Example
img.tooltip= " Your Text : \n"
img.tooltip += " Your text \n";
这将为我工作,它在后面的代码中使用。
希望这对你有用
javascript版本
因为& # 013;(html)是0D(十六进制),这可以表示为'\u000d'
str = str.replace(/\n/g, '\u000d');
此外,
与大家分享一个AngularJS过滤器,它将\n替换为该字符,这要感谢其他答案中的引用
app.filter('titleLineBreaker', function () {
return function (str) {
if (!str) {
return str;
}
return str.replace(/\n/g, '\u000d');
};
});
使用
<div title="{{ message | titleLineBreaker }}"> </div>
如果你使用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 );
},
只需添加一个数据属性
data-html =“真正的”
你可以开始了。
如。用法:
<i data-html="true" class="tooltip ficon-help-icon" twipsy-content-set="true" data-original-title= "<b>Hello</b> Stackoverflow"> </i>
到目前为止,它在我尝试过的大多数工具提示插件中都有效。
只需添加data-html="true"
<a href="#" title="Some long text <br/> Second line text \n Third line text" data-html="true">Hover me</a>
AngularJS with Bootstrap UI Tolltip (uib-tooltip), has three versions of tool-tip:
Uib-tooltip, Uib-tooltip -template和Uib-tooltip -html
- uib-tooltip takes only text and will escape any HTML provided
- uib-tooltip-html takes an expression that evaluates to an HTML string
- uib-tooltip-template takes a text that specifies the location of the template
在我的情况下,我选择了uib-tooltip-html,有三个部分:
配置 控制器 超文本标记语言
例子:
(function(angular) {
//Step 1: configure $sceProvider - this allows the configuration of $sce service.
angular.module('myApp', ['uib.bootstrap'])
.config(function($sceProvider) {
$sceProvider.enabled(false);
});
//Step 2: Set the tooltip content in the controller
angular.module('myApp')
.controller('myController', myController);
myController.$inject = ['$sce'];
function myController($sce) {
var vm = this;
vm.tooltipContent = $sce.trustAsHtml('I am the first line <br /><br />' +
'I am the second line-break');
return vm;
}
})(window.angular);
//Step 3: Use the tooltip in HTML (UI)
<div ng-controller="myController as get">
<span uib-tooltip-html="get.tooltipContent">other Contents</span>
</div>
更多信息请点击这里
使用.html()而不是.text()对我来说很有效。例如
.html("This is a first line." + "<br/>" + "This is a second line.")
只需在脚本中添加以下代码片段:
$(function () {
$('[data-toggle="tooltip"]').tooltip()
});
当然如上所述,data-html的答案应该是“真”。这将允许您在title属性的值内使用html标记和格式。
这个css会帮助你。
.tooltip {
word-break: break-all;
}
or if you want in one line
.tooltip-inner {
max-width: 100%;
}
嗨,这段代码将在所有浏览器中工作!!我使用 用于chrome和safari的新产品线和IE的ul li
function genarateMultiLIneCode(){
var =values["a","b","c"];
const liStart = '<li>';
const liEnd = '</li>';
const bullet = '• ';
var mergedString = ' ';
const unOrderListStart='<ul>'
const unOrderListEnd='</ul>'
const fakeNewline = '
';
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;
}
}
您可以使用引导工具提示,不要忘记将html选项设置为true。
<div id="tool"> tooltip</div>
$('#tool').tooltip({
title: 'line one' +'<br />'+ 'line two',
html: true
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
如果你用的是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 
 next line" href= ""> Hover me </a>
如果你在Django项目中使用,那么我们也可以在工具提示中显示动态数据,比如:
<a class="black-text pb-2 pt-1" data-toggle="tooltip" data-placement="auto" title="{{ post.location }} 
 {{ post.updated_on }}" href= "{% url 'blog:get_user_profile' post.author.id %}">{{ post.author }}</a>