我有一個SVG誰代表了一些容器,我想在每個容器中使用JavaScript動態添加一個十字架。如何在路徑標記svg中指定座標x和y?
我有這樣的代碼在容器中添加一個交叉:
.close-x {
stroke: black;
fill: transparent;
stroke-linecap: round;
stroke-width: 2;
}
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentscripttype="application/ecmascript" contentstyletype="text/css" height="555px" preserveAspectRatio="none" style="width:1181px;height:555px;" version="1.1" viewBox="0 0 1181 555" width="1181px" zoomAndPan="magnify"><defs><filter height="300%" id="f491e1k" width="300%" x="-1" y="-1"><feGaussianBlur result="blurOut" stdDeviation="2.0"></feGaussianBlur><feColorMatrix in="blurOut" result="blurOut2" type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .4 0"></feColorMatrix><feOffset dx="4.0" dy="4.0" in="blurOut2" result="blurOut3"></feOffset><feBlend in="SourceGraphic" in2="blurOut3" mode="normal"></feBlend></filter></defs>
<g>
<!--entity cadvisor-->
<rect fill="#FEFECE" filter="url(#f491e1k)" height="46.29" style="stroke: #A80036; stroke-width: 1.5;" width="97" x="133.675" y="8"></rect>
<rect fill="#FEFECE" height="10" style="stroke: #A80036; stroke-width: 1.5;" width="15" x="210.675" y="13"></rect>
<rect fill="#FEFECE" height="2" style="stroke: #A80036; stroke-width: 1.5;" width="4" x="208.675" y="15"></rect>
<rect fill="#FEFECE" height="2" style="stroke: #A80036; stroke-width: 1.5;" width="4" x="208.675" y="19"></rect>
<text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacingAndGlyphs" textLength="57" x="148.675" y="41.3027">cadvisor</text>
<path class="close-x" d="M 140,12 L 150,25 M 150,12 L 140,25"></path>
</g>
</svg>
但它是靜態的,我想讓它動態。
使用JavaScript我能得到<text>
的位置(我不能提供一個小提琴,因爲它從來沒有工作,我不知道爲什麼,但在我的JS它的工作原理)
var list = document.getElementsByTagName("svg")[0].childNodes[1].childNodes;
console.log(list)
for (var x in list) {
if (list.hasOwnProperty(x)) {
if(list[x].tagName === "text"){
console.log("-----------------");
console.log(list[x]);
console.log("y: ");
console.log(list[x].y.baseVal[0].valueAsString);
console.log("x: ")
console.log(list[x].x.baseVal[0].valueAsString);
//<path class="close-x" d="M 10,10 L 30,30 M 30,10 L 10,30" />
}
}
}
但如果我嘗試要做<path class="close-x" y="81" x="125"></path>
我沒有顯示十字...
我看到一般的想法,但它不會是動態的,對不對?因爲我怎麼才能知道要把'translate(n,nn)'與'
Jerome
(如果你知道如何添加一個十字架與象''我很想知道它太標籤) –
Jerome
沒有交叉標籤,使用'path'標籤是正確的做法。爲了使它動態化,只需使用JS來改變transform屬性。你並不需要更換'text'元素的值 –