How can I use a carriage return in a HTML tooltip?

2018-12-31 15:08发布

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; / &#13;
  • \r\n
  • Environment.NewLine (I'm using C#)

None of the above works. Is it possible?

24条回答
孤独总比滥情好
2楼-- · 2018-12-31 15:22

The latest specification allows line feed character, so a simple line break inside the attribute or entity &#10; (note that characters # and ; are required) are OK.

查看更多
与风俱净
3楼-- · 2018-12-31 15:24

If you are using jQuery :

$(td).attr("title", "One \n Two \n Three");

will work.

tested in IE-9 : working.

查看更多
低头抚发
4楼-- · 2018-12-31 15:25

As for whom &#10; 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

查看更多
荒废的爱情
5楼-- · 2018-12-31 15:26

As a contribution to the &#013; solution, we can also use &#009 for tabs if you ever need to do something like this.

<button title="My to-do list:&#013;&#009;-Item 2&#013;&#009;-Item 3&#013;&#009;-Item 4&#013;&#009;&#009;-Subitem 1">TEST</button>

Demo

enter image description here

查看更多
伤终究还是伤i
6楼-- · 2018-12-31 15:27

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.

查看更多
深知你不懂我心
7楼-- · 2018-12-31 15:28

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'
查看更多
登录 后发表回答