简体   繁体   English

如何在JavaScript工具提示中进行换行

[英]How to make a line break in a javascript tooltip

In this svg file(KLICK ME) I want to have a more detailed tooltip with more line in it. 在这个svg文件(KLICK ME)中,我想有一个更详细的工具提示,其中包含更多行。 So I need some kind of linebreak. 所以我需要某种换行符。 I tried allready \\n , <br> and &#10; 我已经尝试了所有\\n<br>&#10; ;。 with no effect... And I couldn't find anything more on the webs, so I hope some javascript crack may help me :D 没有任何作用...而且我在网上找不到更多内容,所以我希望一些javascript破解技术可以对我有所帮助:D

Here goes my code: 这是我的代码:

<?xml version="1.0" encoding="UTF-8"?>
<svg width="440" height="391" onload="init(evt)" xmlns="http://www.w3.org/2000/svg"  xmlns:xlink="http://www.w3.org/1999/xlink" style="background-color:white;">
  <style>
    .tooltip{
      font-family: Verdana;
      fill:white;
    }
    .tooltip_bg{
      fill: black;
      opacity: 0.5;
    }
  </style>

  <script type="text/ecmascript">
    <![CDATA[
      function init(evt){
        if ( window.svgDocument == null ){
          svgDocument = evt.target.ownerDocument;
        }
        tooltip = svgDocument.getElementById('tooltip');
        tooltip_bg = svgDocument.getElementById('tooltip_bg');
      }
      function ShowTooltip(evt, mouseovertext){
        tooltip.setAttributeNS(null,"x",evt.clientX+11);
        tooltip.setAttributeNS(null,"y",evt.clientY+27);
        tooltip.firstChild.data = mouseovertext;
        tooltip.setAttributeNS(null,"visibility","visible");
        length = tooltip.getComputedTextLength();
        tooltip_bg.setAttributeNS(null,"width",length+8);
        tooltip_bg.setAttributeNS(null,"x",evt.clientX+8);
        tooltip_bg.setAttributeNS(null,"y",evt.clientY+11);
        tooltip_bg.setAttributeNS(null,"visibility","visibile");
      }
      function HideTooltip(evt){
        tooltip.setAttributeNS(null,"visibility","hidden");
        tooltip_bg.setAttributeNS(null,"visibility","hidden");
      }
    ]]>
  </script>

  <a xlink:href="webseite.html" onmousemove="ShowTooltip(evt, 'Tooltip zu 01')" onmouseout="HideTooltip(evt)">
    <rect x="100" y="0" ry="1" width="40" height="50" style="fill:gold;" />
  </a>
  <a xlink:href="webseite.html" onmousemove="ShowTooltip(evt, 'Tooltip zu 01')" onmouseout="HideTooltip(evt)">
    <text x="120" y="25" text-anchor="middle" dominant-baseline="central" font-family="sans-serif">01</text>
  </a>
  <a xlink:href="webseite.html" onmousemove="ShowTooltip(evt, 'Tooltip zu 02')" onmouseout="HideTooltip(evt)">
    <rect x="200" y="0" ry="1" width="40" height="50" style="fill:gold;" />
  </a>
  <a xlink:href="webseite.html" onmousemove="ShowTooltip(evt, 'Tooltip zu 02')" onmouseout="HideTooltip(evt)">
    <text x="220" y="25" text-anchor="middle" dominant-baseline="central" font-family="sans-serif">02</text>
  </a>
  <rect class="tooltip_bg" id="tooltip_bg" x="0" y="0" ry="2" width="55" height="21" visibility="hidden"/>
  <text class="tooltip" id="tooltip" x="0" y="0" visibility="hidden">Tooltip</text>
</svg>

Why not use native toolips which should support this? 为什么不使用应支持此功能的本机工具?

<a xlink:href="webseite.html">
  <title>Toolip
zu b</title>
  <rect x="100" y="0" ry="1" width="40" height="50" style="fill:gold;" />
</a>

Otherwise you'll need to create multiple <text> elements or embed <tspan> elements in the <text> elements so you can assign dy offsets to simulate line breaks. 否则,你就需要创建多个<text>元素或嵌入<tspan>中的元素<text>元素,因此你可以分配DY偏移模拟换行符。 Ie you'll have to code multiple line support yourself from scratch. 即,您将不得不从头开始编写多行支持代码。

You'll need to generate something like 您需要生成类似

<text x="10" y="20">Toolip<tspan x="10" dy="1em">zu b</tspan></text>

where the x and y values vary depending on where your object is. x和y值的位置取决于对象的位置。

Or. 要么。 if you don't care about IE support use a foreignObject element for the tooltips and rely on html's text formatting capability to insert line breaks automatically. 如果您不关心IE的支持,请对工具提示使用foreignObject元素,并依靠html的文本格式设置功能自动插入换行符。

You can use an absolute div with a higher z-index as a tooltip if needs be. 如果需要,可以使用具有较高z-index的绝对div作为工具提示。 This supports any sort of html formatting inside it, as it is just a div tag. 它支持其中的任何html格式,因为它只是一个div标签。

Here is a fiddle to demonstrate: http://jsfiddle.net/pbja2ju6/ 这是演示的小提琴: http : //jsfiddle.net/pbja2ju6/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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