[英]How to get tippy.js to use individual svg elements as targets when the SVG is embedded in an HTML object tag?
I have an svg embedded in HTML using an object tag 我使用对象标签在HTML中嵌入了svg
<div id="svgDiv">
<object id="svgObjectElement" data="mysvg.svg" type="image/svg+xml">
Your browser doesn't support SVG.
</object>
</div>
I want to use the tippy.js library to provide tooltips for circles in my svg, but I can't seem to get tippy to use the circles in mysvg as targets. 我想使用tippy.js库为我的svg中的圈子提供工具提示,但是我似乎并不太想将mysvg中的圈子用作目标。 If the svg is in the HTML (like this line element) it works great.
如果svg位于HTML中(如此line元素),则效果很好。
HTML: HTML:
<svg height="50" width="50"><line x1="0" y1="0" x2="50" y2="50" style="stroke:tomato;stroke-width:5"></line></svg>
<div id="svgDiv">
<object id="svgObjectElement" data="mysvg.svg" type="image/svg+xml">
Your browser doesn't support SVG.
</object>
</div>
JavaScript: JavaScript:
tippy('line', {
content: 'Custom Line Tooltip',
})
I have tried the following JavaScript where circle1 is the id of a circle in the svg but it doesn't work: 我已经尝试了以下JavaScript,其中circle1是svg中的圆圈的ID,但是它不起作用:
var svgDocument = document.getElementById("svgObjectElement").contentDocument;
var element = svgDocument.getElementById("circle1");
tippy(element, {
content: 'Custom circle Tooltip',
});
I have also tried 我也尝试过
var svgDocumentElement= document.getElementById("svgObjectElement").contentDocument.documentElement;
var element = svgDocumentElement.getElementById("circle1");
tippy(element, {
content: 'Custom circle Tooltip',
});
which doesn't work. 这不起作用。 I am using Chrome if that is relevant.
如果相关,我正在使用Chrome。 Any ideas would be appreciated.
任何想法,将不胜感激。
As far as I understand, Tippy.js temporarily appends a div
element at the end of body
tag, inside body tag itself. 据我了解,Tippy.js临时追加
div
在结束元素body
标签,体内标签本身。 The problem with svg
elements is that they neither have body
tag nor they support the div
tag and are therefore not going to show up. svg
元素的问题在于它们既没有body
标签,也不支持div
标签,因此不会显示出来。 See here: Is it possible to append a div inside an SVG element? 参见此处: 是否可以在SVG元素内附加div?
If Tippy.js were able to detect that it is inside an svg
document it could wrap the div
inside foreignobject
tag (which would most probably solve the issue). 如果Tippy.js能够检测到它在
svg
文档中,则可以将div
包装在foreignobject
标签内(这很可能会解决此问题)。
One more thing to notice is that you would also have to include Tippy.js library directly into the svg
document itself like this <script xlink:href="../js/tippy.min.js"></script>
. 需要注意的另一件事是,您还必须像这样
<script xlink:href="../js/tippy.min.js"></script>
一样,将Tippy.js库直接包含到svg
文档中。 But even then you will get an error because Tippy.js is trying to reference elements which do not exist head
, body
, etc. 但是即使那样,您也会收到错误消息,因为Tippy.js试图引用不存在
head
, body
等的元素。
So, to me, the best approach for this problem is to create an overlay div
over your circle1
element outside of the svg
object and add a tooltip to that div
. 所以,对我来说,这个问题的最好的方法是创建一个覆盖
div
在你的circle1
的外部元件svg
对象和工具提示添加到该div
。 Hint: How to overlay one div over another div 提示: 如何将一个div覆盖在另一个div上
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.