㈠ 如何通过js获取HTML中iframe中的SVG整个文档
先找到iframe对象,再通过iframe的contentDocument属性获取SVG整个文档对象
<iframesrc="svg/02.svg"frameborder="0"width="500"height="300"id="f1"></iframe>
varobj=document.getElementById("f1");
vard=obj.contentDocument;
㈡ 如何用 JS 获取 SVG 内 path 元素中的图形的实际位置及尺寸
用 JS 获取 SVG 内 path 元素中的图形的实际位置及尺寸的方法:
1、使用内getElementById获取容svg节点对象:
var el = document.getElementById("yourElement");
2、调用getBoundingClientRect获取path的边界矩形的位置:
var rect = el.getBoundingClientRect();
3、分别获取width和height属性就得到实际位置了。
console.log( rect.width );
console.log( rect.height);
㈢ svg无法加载外部js
<svgwidth="580"height="399.9999999"
xmlns="http://www.w3.org/2000/svg"version="1.2"baseProfile="tiny"
xmlns:ev="http://www.w3.org/2001/xml-events">
<scripttype="application/ecmascript"xlink:href="demo.js"/>
demo.js
//demo.js删除<script></script>
我自己写了一个
文件:a.svg
<svgwidth="100%"height="100%"viewBox="00100100"version="1.2"baseProfile="full"
xmlns="http://www.w3.org/2000/svg"xmlns:xlink="http://www.w3.org/2000/xlink"xmlns:ev="http://www.w3.org/2001/xml-events">
<scripttype="application/ecmascript"href="s.es"/>
<circlecx="50"cy="50"r="45"fill="green"onclick="change(evt)"/>
</svg>
文件:s.es
functionchange(evt){
if(evt.target.getAttribute('fill')==='green'){
evt.target.setAttribute('fill','red')
}else{
evt.target.setAttribute('fill','green')
}
}
㈣ JS正则截取指定svg文本
varstr='<g><path></path></g><gclass="className"><path></path></g><gclass="className"><path></path></g><g><path></path></g>';
varreg=/<g[^>]*class[="'s]+className["']?[^>]*>(?:(?!</g>)[sS])*</g>/gi;
str.match(reg);