SVG Image turns black after updating his path via jQuery

北城以北 提交于 2019-12-10 12:17:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!