这是我拥有的内容:
<path class="..." onmousemove="show_tooltip(event,'very long text
\\\n I would like to linebreak')" onmouseout="hide_tooltip()" d="..."/>
<rect class="tooltip_bg" id="tooltip_bg" ... />
<text class="tooltip" id="tooltip" ...>Tooltip</text>
<script>
<![CDATA[
function show_tooltip(e,text) {
var tt = document.getElementById('tooltip');
var bg = document.getElementById('tooltip_bg');
// set position ...
tt.textContent=text;
bg.setAttribute('width',tt.getBBox().width+10);
bg.setAttribute('height',tt.getBBox().height+6);
// set visibility ...
}
...
现在我的非常长的工具提示文本没有换行,即使我使用alert();它向我显示文本实际上确实有两行。(它包含一个"\",顺便问一下,我该如何删除这个字符?)
无论我在哪里都无法让CDATA起作用。
var tspan = document.createElement('tspan') tspan.setAttribute('x','0'); tspan.setAttribute('dy','1.2em'); tspan.textContent = text; tt.appendChild(tspan);
根本没有显示任何文本。 - sollnissx=0
是一个绝对坐标:将文本片段移动到当前坐标系的原点。g
元素上的transform
属性定义了一个新的当前坐标系,假设文本是左对齐的,那么 tspan 就会向左移动。这就像是回车指令一样。dy=1.2em
是一个相对坐标:将文本碎片相对于当前文本碎片移动这个量。这就像是换行指令一样。结合在一起,你就得到了一个 CR/LF。 - Sergiu Dumitriux = 0 dy = 1.2em
更简单,对于每一行都是相同的,而且不会依赖于字体大小。另一个优点是,如果你想移动整个文本,你只需要更新组的“transform”属性中的一对值,而不是更新每个tspan的坐标。 - Sergiu Dumitriu