如何删除HTML中的SVG 如题,我在HTML中用embed标签引入了一个SVG文档,现在想在HTML的unload事件中删除这个SVG文档,主要原因是IE这个王八蛋。我刷HTML时,HTML中的SVG并没有被unload,就导致了每刷新一次HTML,IE占用内存就增加一次,一直不停…… 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 var embeds = document.getElementsByTagName("embed");while(embeds.length > 0) { embeds[0].parentNode.removeChild(embeds[0]);} 谢谢楼上,这个我试过了,没用,关键就是svg文档没有删除,svg文档也有unload事件,我在事件中设置了一个弹出,在刷新HTML时firefox,opera,chrome都能够正常弹出,就IE不行。我的理解就是加载HTML时把svg文档也加载到内存中,那三个浏览器在刷新HTML时先从内存中删除HTML文档和svg文档,然后再加载HTML文档、再加载svg文档。现在问题就是在卸载HTML文档时IE不会主动去卸载HTML中的svg文档,所以内存就一直在增加,我就想找个方法手动去卸载掉那个svg文档,但是又查不到资料……也不知道理解的对不对…… 楼主,好像没有这种情况啊,我引用svg也用embed标签,但是不会在刷新的时候增加IE占用的内存……不知道你的svg结构是什么样的 谢谢楼上,我把代码贴出来,麻烦你帮我看看,谢谢。svg:<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" onload="init(evt);" onmousemove="findNearest(evt);" onunload="unload();"> <style type="text/css"><![CDATA[ svg{ fill:none; stroke:black; stroke-width:1; } ]]> </style> <script type="text/javascript" xlink:href="svg.js"></script> <rect x="-1" y="-1" width="105%" height="105%" fill="none" pointer-events="all"></rect> <g id="content"> </g></svg>SVG中引用的javascript:var Ext,myExt,SVG={},doc,root,ns;/** * 查找最近的点,并获取描述 * @param {} e */findNearest=function(e){ var el=e.target, child=el.firstChild, desc; while(child){ if(child.nodeName=='desc'){ try{ alert(getText(child)); desc=Ext.decode(getText(child)); } catch(ex){alert(ex.message);} break; } child=child.nextSibling; }};/** * svg文档加载初始化,主要为svg文档与html文档建立起联系 * @param {} e */init=function(e){ doc=e.target.ownerDocument; root=doc.documentElement; ns=root.namespaceURI; doc.window=window; var parent=window.parent; Ext=parent.Ext; myExt=parent.myExt; Ext.applyIf(Number.prototype,parent.Number.prototype); Ext.applyIf(Date.prototype,parent.Date.prototype);};unload=function(evt){ alert('unloadSVG');};/** * 加载数据 */loadData=function(cfg,w,h){ ……}; 表示帮不到你……不过建议你试试在html中调用svg的函数,在svg里写个函数释放掉svg里对象、函数的引用变量,可能是这个问题。另外,在IE中貌似svg的unload事件不能触发吧,我是个菜菜,我没捕获过svg的unload事件…… 在Html的unload或者beforeunload里调用IE有个bug,可能你remove了,内存仍然没有释放。 莫非真是loveajin(这个哥们也喜欢阿金? : ) )所说的,我在svg里写了个函数,把最前面定义的变量全部指向null,再刷新时内存基本不增加,不过第一次还是加了些,虽然量不大,不过后面到是基本不增加了,OYE,再测试测试 我的菜单跟幻灯片的js冲突有人能帮我吗?在线等,急救啊 请问如何能下载该网站的上的2个flash (flash路径加密技术) 求助json.js问题!很急高手进! 能否用javascript调用通用对话框?? 嵌套div拖曳 请高手指教:如何让IE浏览器上的“前进”“后退”“刷新”失效??? 预览缩略图怎么实现啊! 求个function,使该文本框不能输入小于等于0的数 求一个日期格式函数? 高版本的IE存在BUG?? 这是神马意思 extjs 如何修改GridPanel RowSelectModel默认选中行的颜色
embeds[0].parentNode.removeChild(embeds[0]);
}
svg:<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
onload="init(evt);" onmousemove="findNearest(evt);" onunload="unload();">
<style type="text/css"><![CDATA[
svg{
fill:none;
stroke:black;
stroke-width:1;
}
]]>
</style>
<script type="text/javascript" xlink:href="svg.js"></script>
<rect x="-1" y="-1" width="105%" height="105%" fill="none" pointer-events="all"></rect>
<g id="content">
</g>
</svg>SVG中引用的javascript:var Ext,myExt,SVG={},doc,root,ns;
/**
* 查找最近的点,并获取描述
* @param {} e
*/
findNearest=function(e){
var el=e.target,
child=el.firstChild,
desc;
while(child){
if(child.nodeName=='desc'){
try{
alert(getText(child));
desc=Ext.decode(getText(child));
}
catch(ex){alert(ex.message);}
break;
}
child=child.nextSibling;
}
};
/**
* svg文档加载初始化,主要为svg文档与html文档建立起联系
* @param {} e
*/
init=function(e){
doc=e.target.ownerDocument;
root=doc.documentElement;
ns=root.namespaceURI;
doc.window=window;
var parent=window.parent;
Ext=parent.Ext;
myExt=parent.myExt;
Ext.applyIf(Number.prototype,parent.Number.prototype);
Ext.applyIf(Date.prototype,parent.Date.prototype);
};
unload=function(evt){
alert('unloadSVG');
};
/**
* 加载数据
*/
loadData=function(cfg,w,h){
……
};
在Html的unload或者beforeunload里调用
IE有个bug,可能你remove了,内存仍然没有释放。