使用JavaScript努力创建SVG形状
问题描述:
我已经阅读了其他与使用JavaScript创建SVG相关的问题,并且关注了任何链接,但我似乎无法得到它的工作。也许这里有人可以发现我要出错的地方。使用JavaScript努力创建SVG形状
编辑:我目前有一个与ID'svgbasics'在文档正文的div,我试图使用我从XML文档中检索到的值创建一个SVG形状。
当我加载页面时,什么都不显示。
这里是我的代码:
var questions = [];
for (j=0; j<arrayIds.length; j++)
{
$(xml).find("C[ID='" + arrayIds[j] + "']").each(function(){
// pass values
questions[j] =
{
typ: $(this).attr('typ'),
width: $(this).find("I").attr('wid'),
height: $(this).find("I").attr('hei'),
x: $(this).find("I").attr('x'),
y: $(this).find("I").attr('x'),
baC: $(this).find("I").attr('baC'),
boC: $(this).find("I").attr('boC'),
boW: $(this).find("I").attr('boW')
}
if ($(this).attr('typ') == '3')
{
var shape = this.id;
var svg = $('#svgbasics').svg('get');
svg.rect(x($(this).find("I").attr('x')),
y($(this).find("I").attr('y')),
width($(this).find("I").attr('width')),
height($(this).find("I").attr('height')),
{fill: colours[$(this).find("I").attr('baC')]});
} else {
// Add here
alert($(this).find("I").attr('x'));
}
});
}
答
这是相当很难说什么是错的,而无需完整的上下文。我首先建议在DOM/JS调试器中检查您的内容,例如Opera Dragonfly,Webkit Web Inspector或Mozilla Firebug。验证所有元素的创建是否正确,并且它看起来像您在DOM树中所期望的那样。
如果您已经使用JavaScript,难道不会更容易使用
我选择使用SVG的原因是因为我打算使用SVG形状来构建一个UI,并且从我的理解来看,SVG是比Canvas更好的事件处理选择。 – 2010-06-04 12:07:38