【发布时间】:2014-02-16 08:12:36
【问题描述】:
我正在使用 Javascript 计算 svg 元素内的 path 和 circle 的值。我可以将正确的 HTML 代码注入 HTML 页面,但没有绘制圆/svg。
这是由 Javascript 注入到我的 HTML 中的内容
<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文件中(即不使用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。谢谢。
标签: javascript html angularjs svg