问题
I have the following html code
<div>
<a id="cover"></a>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" viewBox="0 0 510 680">
<rect x="0" y="0" fill="#000007" width="510" height="680"/>
<image width="510" height="680" xlink:href="../images/MSRCover.png" transform="translate(0 0)" />
</svg>
</div>
I am trying to change the image's path with jQuery and the image turns black.
$ = cheerio.load(data);
$('image').each(function()
{
var $img = $(this);
$(this).attr('xlink:href','My PATH').html();
});
I am using node.js and the module cheerio.
Thanks
回答1:
the jquery .attr method does not understand namespaces. use normal DOM setAttributeNS instead e.g.
$(this)[0].setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', "My PATH");
来源:https://stackoverflow.com/questions/22081551/svg-image-turns-black-after-updating-his-path-via-jquery