簡體   English   中英

訪問和操作 object 標簽內的 svg 組

[英]Acess and manipulate groups of an svg that is inside of an object tag

例如,我有這個我知道如何操作的代碼:

<g id="box">
   <path class="st4" d="M893,577H359c-6.6,0-12-5.4-12-12V323c0-6.6,5.4-12,12-12h534c6.6,0,12,5.4,12,12v242
       C905,571.6,899.6,577,893,577z"/>
</g>
</svg>


<!--External calls-->
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>


</html>

我可以用這個 java 腳本 object 操作組“框”:

const svg = document.getElementById("box");

但如果我這樣做怎么辦:

<html>
    <object data="my_svg.svg"></object>


<!--External calls-->
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>


</html>

我怎樣才能在這個 object 標簽中訪問這個 svg 里面的盒子組?

只要 SVG 與您的頁面同源,您就可以通過對象的contentDocument訪問它。

<html>
    <object data="my_svg.svg" id="box"></object>
</html>
document.getElementById("box").contentDocument.children[0] // should be the svg element

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM