代码之家  ›  专栏  ›  技术社区  ›  DesVal

向SVG矩形标记添加工具提示

  •  9
  • DesVal  · 技术社区  · 11 年前

    向中添加工具提示的最佳解决方案是什么 <rect> ?

    我用几个 <矩形> 标签,我想在鼠标悬停时显示工具提示。使用 title 属性很好,但是否有任何选项可以使用CSS/Javascript/jQuery重新设置其样式,或者是否有更好的选项可以添加工具提示?

    <svg version="1.1" baseProfile="basic" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 50 50" xml:space="preserve">
        <g>
            <rect id="1" title="There's some text" x="0" y="0" fill="#666666" width="20" height="20"/>
            <rect id="2" title="There's another text" x="30" y="0" fill="#666666" width="20" height="20"/>
        </g>
    </svg>
    
    2 回复  |  直到 11 年前
        1
  •  21
  •   Robert Longson    11 年前

    SVG使用标题元素,而不是属性 不能 尽管如此,还是要为它们设计风格。如果需要设置样式,则需要将标题动态创建为 <text> 元素,并使用javascript将其放置在正确的位置。

    这是默认工具提示的样子。。。

    <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 50 50" xml:space="preserve">
        <g>
            <rect id="1" fill="#666666" width="20" height="20">
              <title>There's some text</title>
            </rect>
            <rect id="2" x="30" fill="#666666" width="20" height="20">
              <title>There's another text</title>
            </rect>
       </g>
    </svg>
        2
  •  1
  •   Community Mohan Dere    9 年前

    我试过你的代码,但不起作用(Chrome)。然后我在这个网站上找到了一篇关于工具提示的帖子:

    How to add a tooltip to an svg graphic?

    因此,您需要这样做,而不是添加title属性:

    <rect id="1" x="0" y="0" fill="#666666" width="20" height="20"><title>"There's some text"</title/></rect>
    

    更新

    您可以使用javascript和jquery更改文本和样式

    例子:

    $("#1").find("title").html("Text")