SVG鼠标事件在Firefox4中起作用,但在IE8中不起作用
作者:互联网
我有一个独立的SVG文件和一个单独的Javascript文件,用于处理从SVG文件触发的鼠标事件.该项目与FF配合得很好,但是在管理鼠标事件时我以某种方式遇到了IE问题:我收到以下错误消息:“ clientx为null或不是对象”. SVG图像虽然可以打印.
你们知道我的代码有什么问题(见下文)吗?
谢谢
SVG文件
<?xml version="1.0" standalone="no"?>
<svg width="1100" height="5990" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" onl oad="init(evt)" >
<script xlink:href="desc.js"/>
<g onm ouseout="h(evt)">" stroke-width="1"/>
<polygon points="35,20 86,20 96,35 86,50 35,50" style="fill:grey" onm ousemove="s(evt,'someTxt')" onclick="m(evt, 'NGR_a00010')"/>
<polygon points="99,20 138,20 148,35 138,50 99,50" style="fill:grey" onm ousemove="s(evt,'someTxt someTxt')" onclick="m(evt, 'NGR_a00020')"/>
</g>
<rect class="tooltip_bg" id="tooltip_bg" x="0" y="0" rx="4" ry="4" width="55" height="17" visibility="hidden"/>
<text class="tooltip" id="tooltip" x="0" y="0" visibility="hidden">Tooltip</text>
</svg>
使用Javascript
function init(evt)
{
if ( window.svgDocument == null )
{
svgDocument = evt.target.ownerDocument;
}
tooltip = svgDocument.getElementById('tooltip');
tooltip_bg = svgDocument.getElementById('tooltip_bg');
}
function s(evt, mouseovertext)
{
var posx = 0;
var posy = 0;
if (!evt) var e = window.event;
if (evt.pageX || evt.pageY) {
posx = evt.pageX;
posy = evt.pageY;
}
else if (e.clientX || e.clientY) {
posx = evt.clientX + document.body.scrollLeft
+ document.documentElement.scrollLeft;
posy = evt.clientY + document.body.scrollTop
+ document.documentElement.scrollTop;
}
tooltip.setAttributeNS(null,"x",posx+11);
tooltip.setAttributeNS(null,"y",posy+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",posx+8);
tooltip_bg.setAttributeNS(null,"y",posy+14);
tooltip_bg.setAttributeNS(null,"visibility","visibile");
}
function h(evt)
{
tooltip.setAttributeNS(null,"visibility","hidden");
tooltip_bg.setAttributeNS(null,"visibility","hidden");
}
function g(evt, locus_tag)
{
window.open("http://www.ncbi.nlm.nih.gov/gene?term=" + locus_tag);
}
function m(evt, txt)
{
if (evt.type == "click" && evt.detail == 2)//if we got a double click, for some reason onblclick does not work
{
window.open("http://www.ncbi.nlm.nih.gov/gene?term=" + txt);
}
}
解决方法:
IE8不支持SVG.
有可用的Javascript工具可以帮助您解决此问题,但本机不支持它.
在我提到的工具中,我最喜欢的是Raphael,但是Raphael是一个用于绘制图形的库.由于您的代码中已经包含SVG,因此您可能会发现一个简单的转换库更有用.可能是其中之一:http://code.google.com/p/svg2vml/或以下:http://code.google.com/p/svgweb/
既然您说的是SVG图片在您的页面中正常工作,我想说的是您可能已经在使用其中一个或其他工具(可能是我上面链接的工具之一,也可能是另一个)-其中有很多工具).但是我的猜测是您使用的工具不包括使用Javascript操纵SVG对象的支持.
因此,如果需要此功能,可能需要尝试其他工具.
标签:javascript,firefox,svg,javascript-events,internet-explorer-8 来源: https://codeday.me/bug/20191012/1902472.html