通过jQuery更新路径后,SVG Image变黑 [英] SVG Image turns black after updating his path via jQuery

查看:72
本文介绍了通过jQuery更新路径后,SVG Image变黑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下HTML代码

<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>

我试图用jQuery改变图像的路径,图像变黑。

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();
});

我正在使用node.js和模块cheerio。

I am using node.js and the module cheerio.

谢谢

推荐答案

jquery .attr方法无法理解名称空间。例如,使用普通的DOM setAttributeNS

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");

这篇关于通过jQuery更新路径后,SVG Image变黑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆