可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I\'m currently adding verbose tooltips to our site, and I\'d like (without having to resort to a whizz-bang jQuery plugin, I know there are many!) to use carriage returns to format the tooltip.
To add the tip I\'m using the title
attribute. I\'ve looked around the usual sites and using the basic template of:
<a title=\'Tool?Tip?On?New?Line\'>link with tip</a>
I\'ve tried replacing the ?
with:
<br />
&013; /
\\r\\n
Environment.NewLine
(I\'m using C#)
None of the above works. Is it possible?
回答1:
It\'s so simple you\'ll kick yourself... just press enter!
<a title=\'Tool
Tip
On
New
Line\'>link with tip</a>
Firefox won\'t display multi-line tooltips at all though - it will replace the newlines with nothing.
回答2:
The latest specification allows line feed character, so a simple line break inside the attribute or entity
(note that characters #
and ;
are required) are OK.
回答3:
Try character 10. It won\'t work in Firefox though. :(
The text is displayed (if at all) in a
browser dependent manner. Small
tooltips work on most browsers. Long
tooltips and line breaking work in IE
and Safari (use
or
for a
new newline). Firefox and Opera do not
support newlines. Firefox does not
support long tooltips.
http://modp.com/wiki/htmltitletooltips
Update:
As of January 2015 Firefox does support using
to insert a line break in an HTML title
attribute. See the snippet example below.
<a href=\"#\" title=\"Line 1 Line 2 Line 3\">Hover for multi-line title</a>
回答4:
Tested this in IE, Chrome, Safari, Firefox (latest versions 2012-11-27):
Works in all of them...
回答5:
Also worth mentioning, if you are setting the title attribute with Javascript like this:
divElement.setAttribute(\"title\", \"Line one Line two\");
It won\'t work. You have to replace that ASCII decimal 10 to a ASCII hexadecimal A in the way it\'s escaped with Javascript. Like this:
divElement.setAttribute(\"title\", \"Line one\\x0ALine two\");
回答6:
As of Firefox 12 they now support line breaks using the line feed HTML entity:
<span title=\"First line Second line\">Test</span>
This works in IE and is the correct according to the HTML5 spec for the title attribute.
回答7:
If you are using jQuery :
$(td).attr(\"title\", \"One \\n Two \\n Three\");
will work.
tested in IE-9 : working.
回答8:
As a contribution to the 
solution, we can also use 	
for tabs if you ever need to do something like this.
<button title=\"My to-do list:
	-Item 2
	-Item 3
	-Item 4
		-Subitem 1\">TEST</button>
Demo
回答9:
I know I\'m late to the party, but for those that just want to see this working, here\'s a demo: http://jsfiddle.net/rzea/vsp6840b/3/
HTML used:
<a href=\"#\" title=\"First Line
Second Line\">Multiline Tooltip</a>
<br>
<br>
<a href=\"#\" title=\"List:
• List item here
• Another list item here
• Aaaand another list item, lol\">Unordered list tooltip</a>
回答10:
I don\'t believe it is. Firefox 2 trims long link titles anyway and they should really only be used to convey a small amount of help text. If you need more explanation text I would suggest that it belongs in a paragraph associated with the link. You could then add the tooltip javascript code to hide those paragraphs and show them as tooltips on hover. That\'s your best bet for getting it to work cross-browser IMO.
回答11:
will work in i.e. only
回答12:

<----- This is the text needed to insert Carry Return.
回答13:
On Chrome 16, and possibly earlier versions, you can use \"\\n\". As a sidenote, \"\\t\" also works
回答14:
As for whom
didn\'t work you have to style the element on which lines are visible in as : white-space: pre-line;
Referenced from this Answer : https://stackoverflow.com/a/17172412/1460591
回答15:
We had a requirement where we needed to test all of these, here is what I wish to share
document.getElementById(\"tooltip\").setAttribute(\"title\", \"Tool\\x0ATip\\x0AOn\\x0ANew\\x0ALine\")
<p title=\'Tool
Tip
On
New
Line\'>Tooltip with <pre>
new
line</pre> Works in all browsers</p>
<hr/>
<p title=\"Tool Tip On New Line\">Tooltip with <code>&#13;</code> Not works Firefox browsers</p>
<hr/>
<p title=\'Tool Tip On New Line\'>Tooltip with <code>&#10;</code> Works in some browsers</p>
<hr/>
<p title=\'Tool
Tip
On
New
Line\'>Tooltip with <code>&#xD;</code> May work in some browsers</p>
<hr/>
<p id=\'tooltip\'>Tooltip with <code>document.getElementById(\"tooltip\").setAttribute(\"title\", \"Tool\\x0ATip\\x0AOn\\x0ANew\\x0ALine\")</code> May work in some browsers</p>
<hr/>
<p title=\"List:
• List item here
• Another list item here
• Aaaand another list item, lol\">Tooltip with <code>• </code>Unordered list tooltip</p>
<hr/>
<p title=\'Tool\\nTip\\nOn\\nNew\\nLine\'>Tooltip with <code>\\n</code> May not work in modern browsers</p>
<hr/>
<p title=\'Tool\\tTip\\tOn\\tNew\\tLine\'>Tooltip with <code>\\t</code> May not work in modern browsers</p>
<hr/>
<p title=\'Tool
Tip
On
New
Line\'>Tooltip with <code>&#013;</code> Works in most browsers</p>
<hr/>
Fiddle
回答16:
Just use this:
<a title=\'Tool
Tip
On
New
Line\'>link with tip</a>
You can add new line on title by using this 

.
回答17:
Just use JavaScript. Then compatible with most and older browsers.
Use the escape sequence \\n for newline.
document.getElementById(\"ElementID\").title = \'First Line text \\n Second line text\'
回答18:
From the information available on accessibility and the use of tooltips making them larger with the use of CR or line break is appreciated, the fact that the various browsers cannot/will not agree on basics shows that they don\'t much care about accessibility.
回答19:
According to this article on the w3c website:
CDATA is a sequence of characters from the document character set and
may include character entities. User agents should interpret attribute
values as follows:
- Replace character entities with characters,
- Ignore line feeds,
- Replace each carriage return or tab with a single space.
This means that (at least) CR and LF won\'t work inside title attribute. I suggest that you use a tooltip plugin. Most of these plugins allow arbitrary HTML to be displayed as an element\'s tooltip.
回答20:
This 
should work if you just use a simple title attribute.
on bootstrap popovers, just use data-html=\"true\"
and use html in the data-content
attribute .
<div title=\"Hello 
World\">hover here</div>
回答21:
Much nicer looking tooltips can be created manually, and can include HTML formatting.
<!DOCTYPE html>
<html>
<style>
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
bottom: 125%;
left: 50%;
margin-left: -60px;
opacity: 0;
transition: opacity 0.3s;
}
.tooltip .tooltiptext::after {
content: \"\";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #555 transparent transparent transparent;
}
.tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
}
</style>
<body style=\"text-align:center;\">
<h2>Tooltip</h2>
<p>Move the mouse <a href=\"#\" title=\"some text
more and then some\">over</a> the text below:</p>
<div class=\"tooltip\">Hover over me
<span class=\"tooltiptext\">Tooltip text
some <b>more</b><br/>
<i>and</i> more</span>
</div>
<div class=\"tooltip\">Each tooltip is independent
<span class=\"tooltiptext\">Other tooltip text
some more<br/>
and more</span>
</div>
</body>
</html>
This is taken from the w3schools post on this. Experiment with the above code here.
回答22:
Razor Syntax
In the case of ASP.NET MVC you can just store the title as a variable as use \\r\\n
and it\'ll work.
@{
var logTooltip = \"Sunday\\r\\nMonday\\r\\netc.\";
}
<h3 title=\"@logTooltip\">Logs</h3>
回答23:
hack but works - (tested on chrome and mobile)
just add no break spaces till it breaks - you might have to limit the tooltip size depending on the amount of content but for small text messages this works:
etc
Tried everything above and this is the only thing that worked for me -
回答24:
use data-html=\"true\"
and apply <br>
.