javascript 是否可以在此工具提示中插入换行符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3577951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
is it possible to insert a line break in this tooltip?
提问by l--''''''---------''''''''''''
i have a pretty map of the US:
我有一张漂亮的美国地图:
http://upload.wikimedia.org/wikipedia/commons/a/a5/Map_of_USA_with_state_names.svg
http://upload.wikimedia.org/wikipedia/commons/a/a5/Map_of_USA_with_state_names.svg
i would like to implement a tooltip with multiline functionality such as here over the red square:
我想实现一个具有多行功能的工具提示,例如在红色方块上:
http://www.carto.net/papers/svg/gui/tooltips/
http://www.carto.net/papers/svg/gui/tooltips/
please help me get started on this. i will be offering a maximum bounty on this.
请帮我开始做这件事。我将为此提供最大的赏金。
采纳答案by palswim
You never cease to amaze me, I__! How many projects are you working on?
你永远不会停止让我惊讶,我__!你在做多少项目?
Tooltips are browser-generated quick popups; you should use \nto set the .titleattribute in JavaScript.
工具提示是浏览器生成的快速弹出窗口;您应该使用在 JavaScript 中\n设置.title属性。
回答by Chase Florell
Why not use some jQuerymagic and use the title "Attribute"instead of a Title "Tag"?
I think you're confusing the two in your original question
为什么不使用一些jQuery魔法并使用标题“属性”而不是标题“标签”?
我认为您在原始问题中混淆了两者
Check out this jsFiddle
看看这个 jsFiddle
I've also used the jQuery Tools Tooltip
我还使用了jQuery 工具工具提示
Markupnote: Basically all YOU need to do is add the titleattribute and the addtooltipclass to any page element that you want to add a tooltip to. Then you can use CSS to style the tooltip however you see fit.
标记说明:基本上您需要做的就是将title属性和addtooltip类添加到您想要添加工具提示的任何页面元素。然后您可以使用 CSS 来设置工具提示的样式,但您认为合适。
<path
id="AK"
class="addtooltip"
style="fill:#f2d0ff"
onmousemove="GetTrueCoords(evt);"
title="this sis ak<br>with a line break">
</path>
CSS
CSS
.tooltip {
display:none;
background-color:black;
font-size:12px;
height:40px;
width:160px;
padding:25px;
color:#fff;
}
Script(bottom of your page)
脚本(页面底部)
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"> </script>
<script src="http://cdn.jquerytools.org/1.2.4/full/jquery.tools.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$(".addtooltip").tooltip();
}); // closes document.ready
</script>
回答by Steve-O-Rama
Apparently, this is a known problem and you have to handle it like this:
显然,这是一个已知问题,您必须像这样处理它:
<desc>
<tspan x="10" y="10">An orange</tspan>
<tspan x="10" y="24.5">circle</tspan>
</desc>
回答by Abhi
I achieved this by creating the tooltip value with <br/>from my server side.
我通过<br/>从我的服务器端创建工具提示值来实现这一点。
回答by Matt Ball
Don't.
别。
The title of the page is handed off to the browser and not rendered in the page; usually that means it shows up in the application's title bar. Actually rendering a newline would make the title bar twice as tall (or more, with multiple newlines) - and that's not going to happen.
页面的标题传递给浏览器而不是在页面中呈现;通常这意味着它显示在应用程序的标题栏中。实际上渲染换行符会使标题栏高两倍(或更多,有多个换行符) - 这不会发生。
回答by Ray
A line break in html is <br />. You can add that anywhere you like.
html 中的换行符是<br />. 你可以在任何你喜欢的地方添加它。
FYI - there is no tag named 'field1', so your getElementsByTagName call will find nothing.
仅供参考 - 没有名为“field1”的标签,因此您的 getElementsByTagName 调用将一无所获。
回答by epascarello
Add a <br/>where you want the break.
添加一个<br/>你想要休息的地方。
回答by Jazzinghen
Well, this depends on where do you want that Line Break to show...
If it should be seen on the page you should add a <br />, if you need that to show only on the page code, you should add a \n.
if you want to do the thing you've asked you simply need to replace in your code the ,in <br />if this text will be shown in an HTML ToolTip. If this will be shown in a Javascript Alert you should use \n.
嗯,这取决于您希望该换行符显示在何处...
如果应该在页面上看到它,您应该添加一个<br />,如果您只需要在页面代码上显示它,您应该添加一个\n.
如果您想做您要求的事情,您只需在代码中替换,in<br />如果此文本将显示在 HTML 工具提示中。如果这将显示在 Javascript Alert 中,您应该使用\n.
回答by Francis Hemsher
The mouseover an SVG element, with a <title>child element is the easiest choice, when it can be shortened. Both FF/Chrome will extend it in a single line. IE will automatically limit it to about 50 characters. The following will take a looooong tooltip and break its line, inserting line breaks(\n) at the desired location between words. The String.prototpye function can be applied before/after filling the <title>text.
鼠标悬停在一个 SVG 元素上,带有 <title>子元素是最简单的选择,当它可以被缩短时。FF/Chrome 都将在一行中扩展它。IE 会自动将其限制为大约 50 个字符。以下将使用 looooong 工具提示并换行,在单词之间的所需位置插入换行符 (\n)。String.prototpye 函数可以在填充<title>文本之前/之后应用。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>SVG ToolTip line breaks</title>
</head>
<body>
<center>
Three Elements Contained in a <g>, plus a <title> element,<br /> used as its tooltip.<br />
<svg width=400 height=400>
<g>
<circle cx=150 cy=150 r=60 fill=red />
<rect x=200 y=200 width=60 height=60 fill=lime />
<ellipse cx=300 cy=300 rx=60 ry=20 fill=blue />
<title id=myToolTip>This is a tool tip for this symbol (a group of elements), that will help describe what it is</title>
</g></svg>
<br /><button onClick=wrapToolTip(25)>wrap tooltip</button>
</center>
</body>
<script>
/* --- Adds SVG line break at space between words when it would exceed the max length
(or if a single loooong word exceeds the Max Line length)
Also usable for HTML line break by including true arg---
*/
String.prototype.fullWrap= function(maxLineLength,htm){
var str1, tem, ax, diff, lim, S= [], br;
var A= this.split(/\s*\//);
while(A.length){
str1= A.shift();
while(str1 && str1.length> maxLineLength){
if(ax=== 0 && /^\S/.test(str1)) S[S.length-1]+= '-';
tem= str1.substring(0, maxLineLength);
ax= tem.lastIndexOf(' ')+ 1;
if(ax== 0){
S.push(str1.substring(0, maxLineLength-1));
str1= str1.substring(maxLineLength-1);
}
else{
tem= str1.substring(0, ax);
str1= str1.substring(ax);
S.push(tem);
}
}
if(str1) S.push(str1);
}
if(htm)
br="<br/>" //--html--
else
br="\n" //----text line break
return S.join(br);
}
function wrapToolTip(maxLen)
{
myToolTip.firstChild.data=myToolTip.firstChild.data.fullWrap(maxLen)
}
</script>
</html>

