简体   繁体   中英

is it possible to insert a line break in this tooltip?

i have a pretty map of the US:

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/

please help me get started on this. i will be offering a maximum bounty on this.

Why not use some jQuery magic and use the title "Attribute" instead of a Title "Tag" ?
I think you're confusing the two in your original question

Check out this jsFiddle

http://jsfiddle.net/s5qUw/2/

I've also used the jQuery Tools Tooltip

Markup note: Basically all YOU need to do is add the title attribute and the addtooltip class 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.

<path
   id="AK"
   class="addtooltip"
   style="fill:#f2d0ff" 
   onmousemove="GetTrueCoords(evt);"
   title="this sis ak<br>with a line break">
</path>

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>

我通过创建工具提示值达到了这个<br/>从我的服务器端。

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.

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>

You never cease to amaze me, I__! How many projects are you working on?

Tooltips are browser-generated quick popups; you should use \\n to set the .title attribute in JavaScript.

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.

<!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 &lt;g>, plus a &lt;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>

A line break in html is <br /> . You can add that anywhere you like.

FYI - there is no tag named 'field1', so your getElementsByTagName call will find nothing.

添加<br/>你想要休息。

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM