Path 路径SVG的LinearGradient

Path 路径SVG的LinearGradient,path,svg,linear-gradients,Path,Svg,Linear Gradients,我需要填充一个使用javascript动态创建的路径。我已经为它创建了一个渐变,并用它来填充我的路径,但什么也没发生。我的代码怎么了 $('svg').prepend('<defs id="gradient"></defs>'); $('#gradient').append('<linearGradient id="yellow" x1="0%" y1="0%" x2="100%" y2="0%"> </linearGradient>'); $(

我需要填充一个使用javascript动态创建的路径。我已经为它创建了一个渐变,并用它来填充我的路径,但什么也没发生。我的代码怎么了

$('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)');`
$('svg')。前缀(“”);
$('#gradient')。附加('');
$('#黄色')。附加('');
$('g:n个子(2n+1)路径').attr('fill','url(#gradient')`

您的填充指向defs元素,而不是线性半径。id=“gradient”应该位于linearGradient元素上,或者将填充url设置为黄色