2012-04-19 75 views
0

我需要填充使用javascript动态创建的路径。我为此创建了一个渐变,并用它来填充我的路径,但没有发生任何事情。我的代码有什么问题?用于路径SVG的LinearGradient

$('svg').prepend('<defs id="gradient"></defs>'); 
$('#gradient').append('<linearGradient id="yellow" x1="0%" y1="0%" x2="100%" y2="0%"> </linearGradient>'); 
$('#yellow').append('<stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" /> <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />'); 
$('g:nth-child(2n+1) path').attr('fill','url(#gradient)');` 

回答

0

您的填充指向的是defs元素而不是linearGradient。 id =“gradient”应该位于linearGradient元素上 - 或者填充url(#黄色)