我有一个独立的SVG文件和一个单独的Javascript文件来处理SVG文件触发的鼠标事件。这些项目在Firefox上运行良好,但不知何故,我在管理鼠标事件时遇到了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" onload="init(evt)" >
<script xlink:href="desc.js"/>
<g onmouseout="h(evt)">" stroke-width="1"/>
<polygon points="35,20 86,20 96,35 86,50 35,50" style="fill:grey" onmousemove="s(evt,'someTxt')" onclick="m(evt, 'NGR_a00010')"/>
<polygon points="99,20 138,20 148,35 138,50 99,50" style="fill:grey" onmousemove="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工具可以帮助实现这一点,但它本身并不支持它
在我提到的工具中,我最喜欢的是拉斐尔,但拉斐尔是一个绘制图形的图书馆;由于您的代码中已经有了SVG,您可能会发现一个简单的转换库更有用。也许是这样的:http://code.google.com/p/svg2vml/或者这样:http://code.google.com/p/svgweb/
既然你说SVG图像在你的页面中工作,我想说你可能已经在使用这些工具中的一个或另一个了(可能是我上面链接的一个,可能是另一个——有很多)。但我的猜测是,您使用的工具不支持使用Javascript操作SVG对象。
因此,如果您想要此功能,您可能需要尝试其他工具。
正如Spudley的回答所说,IE8不支持SVG。
如果图像出现在您的页面上,则有几种可能性:
- 开发堆栈中的某些东西可能正在将其转换为光栅图像(如PNG):右键单击图像并选择"属性",然后查看"类型"的报告值
- 您可能安装了一个浏览器插件,用于呈现SVG(但不提供JavaScript接口)。尝试在安全模式下运行IE8
然而,InternetExplorer9确实支持SVG,因此在可能的情况下,IE升级将解决这个问题。