㈠ 如何通過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);