Javascript 以编程方式显示SVG工具提示

Javascript 以编程方式显示SVG工具提示,javascript,html,svg,tooltip,Javascript,Html,Svg,Tooltip,我有一个svg:circle,它下面有一个svg:title元素,因此标题显示为圆的工具提示。如何以编程方式(使用javascript)显示和隐藏此工具提示?由于title元素本身无法以编程方式显示,因此必须创建一个元素并将其适当定位。由于文本没有背景,您需要创建一个作为背景,或者使用过滤器绘制背景。此外,目前还没有可靠的跨浏览器换行(除非您使用的是HTML) 因此,这里有一个粗略的建议作为起点: <svg xmlns="http://www.w3.org/2000/svg" width=

我有一个svg:circle,它下面有一个svg:title元素,因此标题显示为圆的工具提示。如何以编程方式(使用javascript)显示和隐藏此工具提示?

由于title元素本身无法以编程方式显示,因此必须创建一个
元素并将其适当定位。由于文本没有背景,您需要创建一个
作为背景,或者使用过滤器绘制背景。此外,目前还没有可靠的跨浏览器换行(除非您使用的是HTML

因此,这里有一个粗略的建议作为起点:

<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200">
  <filter x="0" y="0" width="1" height="1" id="tooltipBackground">
     <feFlood flood-color="rgba(200,200,200,.5)"/>
     <feComposite in="SourceGraphic"/>
  </filter>

  <circle r="50" cx="100" cy="100">
    <title>my tooltip</title>
  </circle>

  <script type="text/javascript">
    function showCircleTooltip(circle) {
      var title = circle.getElementsByTagName("title")[0];
      if (title) {
        var tooltip = document.createElementNS("http://www.w3.org/2000/svg","text");
        tooltip.textContent = title.textContent;
        tooltip.setAttribute("filter","url(#tooltipBackground)");
        // We're putting the tooltip at the same place as the circle center.
        // Modify this if you prefer different placement.
        tooltip.setAttribute("x",circle.getAttribute("cx"));
        tooltip.setAttribute("y",circle.getAttribute("cy"));
        var transform = circle.getAttribute("transform");
        if (transform) {
          tooltip.setAttribute("transform",transform);
        }
        circle.parentNode.insertBefore(tooltip, circle.nextSibling);
      }
    }

    showCircleTooltip(document.getElementsByTagName("circle")[0])
  </script>
</svg>

我的工具提示
函数showCircleTooltip(圆形){
var title=circle.getElementsByTagName(“title”)[0];
如果(标题){
var tooltip=document.createElements(“http://www.w3.org/2000/svg“,”文本“);
tooltip.textContent=title.textContent;
setAttribute(“过滤器”,“url(#tooltipBackground)”);
//我们将工具提示放置在圆心所在的位置。
//如果您喜欢不同的位置,请修改此选项。
tooltip.setAttribute(“x”,circle.getAttribute(“cx”);
tooltip.setAttribute(“y”,circle.getAttribute(“cy”);
var transform=circle.getAttribute(“transform”);
如果(变换){
setAttribute(“变换”,变换);
}
circle.parentNode.insertBefore(工具提示,circle.nextSibling);
}
}
showCircleTooltip(document.getElementsByTagName(“圆圈”)[0])

.

由于title元素本身无法以编程方式显示,因此必须创建一个
元素并将其适当定位。由于文本没有背景,您需要创建一个
作为背景,或者使用过滤器绘制背景。此外,目前还没有可靠的跨浏览器换行(除非您使用的是HTML

因此,这里有一个粗略的建议作为起点:

<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200">
  <filter x="0" y="0" width="1" height="1" id="tooltipBackground">
     <feFlood flood-color="rgba(200,200,200,.5)"/>
     <feComposite in="SourceGraphic"/>
  </filter>

  <circle r="50" cx="100" cy="100">
    <title>my tooltip</title>
  </circle>

  <script type="text/javascript">
    function showCircleTooltip(circle) {
      var title = circle.getElementsByTagName("title")[0];
      if (title) {
        var tooltip = document.createElementNS("http://www.w3.org/2000/svg","text");
        tooltip.textContent = title.textContent;
        tooltip.setAttribute("filter","url(#tooltipBackground)");
        // We're putting the tooltip at the same place as the circle center.
        // Modify this if you prefer different placement.
        tooltip.setAttribute("x",circle.getAttribute("cx"));
        tooltip.setAttribute("y",circle.getAttribute("cy"));
        var transform = circle.getAttribute("transform");
        if (transform) {
          tooltip.setAttribute("transform",transform);
        }
        circle.parentNode.insertBefore(tooltip, circle.nextSibling);
      }
    }

    showCircleTooltip(document.getElementsByTagName("circle")[0])
  </script>
</svg>

我的工具提示
函数showCircleTooltip(圆形){
var title=circle.getElementsByTagName(“title”)[0];
如果(标题){
var tooltip=document.createElements(“http://www.w3.org/2000/svg“,”文本“);
tooltip.textContent=title.textContent;
setAttribute(“过滤器”,“url(#tooltipBackground)”);
//我们将工具提示放置在圆心所在的位置。
//如果您喜欢不同的位置,请修改此选项。
tooltip.setAttribute(“x”,circle.getAttribute(“cx”);
tooltip.setAttribute(“y”,circle.getAttribute(“cy”);
var transform=circle.getAttribute(“transform”);
如果(变换){
setAttribute(“变换”,变换);
}
circle.parentNode.insertBefore(工具提示,circle.nextSibling);
}
}
showCircleTooltip(document.getElementsByTagName(“圆圈”)[0])

.

你的意思是没有鼠标交互?是的,我想在没有真正鼠标交互的情况下显示工具提示。你的意思是没有鼠标交互?是的,我想在没有真正鼠标交互的情况下显示工具提示