我正在計算使用Javascript在svg
元素內的path
和circle
的值。我可以將正確的HTML代碼注入HTML頁面,但不繪製circle/svg。HTML SVG不繪製(適用於其他頁面)
這就是被Javascript
<svg>
<circle cx="115" cy="110" r="50"></circle>
<path d="M115,110 L115,0 A50,50 1 0,1 190,35 z"></path>
</svg>
注入到我的HTML如果我將此代碼複製並粘貼到一個不同的.html
文件並運行它,我得到的出現形狀(不完全的圓形)。這在我的實際網頁中不會發生。
我可以得到形狀出現,如果我直接在HTML文件中把上面的代碼(即,不使用Javascript函數來動態地計算並注入它)。
我使用Angular.js,如果有差別。
我不認爲有在我的JavaScript錯誤,因爲注入到HTML頁面的代碼在其他地方工作,但我可以張貼我的Javascript代碼,如果這將有助於。沒有任何樣式可以阻止SVG的出現。這幾乎就像我需要告訴瀏覽器重新加載/重新繪製/重新分析HTML,但我不確定。
感謝您的幫助。
編輯:
這是我的javascript代碼。
/*
<svg>
<circle cx="115" cy="115" r="110"></circle>
<path d="M115,115 L115,5 A110,110 1 0,1 190,35 z"></path>
</svg>
*/
Sector.prototype.draw = function() {
// Create our svg element
var svg = document.createElement('svg');
// Create and set up our circle element
var circ = document.createElement('circle');
circ.setAttribute('cx', this.startX);
circ.setAttribute('cy', this.startY);
circ.setAttribute('r', this.radius);
//Create and set up our path element
var p = document.createElement('path');
var dForPath = this.dString(this.startX, this.startY, this.radius, this.angle);
p.setAttribute('d', dForPath);
// Add our circle and path to the svg
svg.appendChild(circ);
svg.appendChild(p);
return svg;
};
和我的使用
var sec = new Sector(115, 115, 110, 7);
var c = sec.draw();
document.getElementById('circleTest').appendChild(c);
// Weird issue. The following line makes the svg appear;
// without it, the shape isn't drawn at all.
// http://stackoverflow.com/questions/21347833/html-svg-not-drawing-works-in-other-pages
document.getElementById('circleTest').innerHTML += '';
@RobertLongson我編輯的問題,包括我的javascript。謝謝。 – matthewpalmer