jquery动态更改svg image xlink:href

前端之家收集整理的这篇文章主要介绍了jquery动态更改svg image xlink:href前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图用不是填充颜色的图像动态地更改多边形的填充.为此,我找到了一种将填充更改为图像的方法,但是我无法将xlink:href URL动态更改为随机URL.
有什么想法吗?谢谢你的帮助!
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0px" y="0px" width="320px" height="480px" viewBox="0 0 320 480" enable-background="new 0 0 320 480" xml:space="preserve" xmlns:xml="http://www.w3.org/XML/1998/namespace">
<pattern patternUnits="userSpaceOnUse" id="pat1" x="10" y="10" 
 width="425" height="319">
    <image id="background" width="100%" height="100%" 
     xlink:href="http://www.w3.org/1999/xlink" />
</pattern>
<g id="Layer_1">
    <polygon class="background"  fill="url(#pat1)" points="265.188,329.922 160.198,358 55.208,329.922 55.208,120.868 265.188,120.868  "/>

</g>
<g id="Layer_2">
    <g id="brand_mark_1_">
        <g>
            <rect x="265.188" y="120.868" fill="#FFFFFF" width="23.812" height="44.378"/>
            <g>
                <path fill="#020304" d="M275.372,149.035c0,0.949,0.77,1.72,1.721,1.72c0.949,1.72-0.771,1.72-1.72      c0-0.023-0.003-0.044-0.004-0.066v-16.261c0.004-0.043,0.007-0.088,0.007-0.134c0-0.95-0.77-1.72-1.719-1.72      c-0.942,0-1.707,0.757-1.72,1.695h-0.005v16.461h0.001C275.374,149.017,275.372,149.025,149.035z"/>
                <circle fill="#F5170D" cx="277.093" cy="153.541" r="1.72"/>
            </g>
        </g>
    </g>
</g>
</svg>


 $(document).ready(function() {
                var randomColor = Math.floor(Math.random()*16777215).toString(16),randomNumber = Math.floor((Math.random()*855)+48),backg = $('#background');
                    $("body").css("background-color",randomColor);
                    backg.attr("http://www.w3.org/1999/xlink","xlink:href","url(http://blob.apliiq.com/sitestorage/fabric/" + randomNumber + ".png)");
                    $('a#cta').attr("href","/designyourown/?fabricID=" + randomNumber );               
            });

解决方法

如果您纠正了backg.attr功能属性,则可以正常工作.
$(document).ready(function () {
    var randomColor = Math.floor(Math.random() * 16777215).toString(16),randomNumber = Math.floor((Math.random() * 855) + 48),backg = $('#background');
    $("body").css("background-color",'#' + randomColor);
    backg.attr("xlink:href","http://blob.apliiq.com/sitestorage/fabric/" + randomNumber + ".png");
    $('a#cta').attr("href","/designyourown/?fabricID=" + randomNumber);
});

JSFiddle链接http://jsfiddle.net/arpitworld/jhetx/

原文链接:https://www.f2er.com/jquery/176003.html

猜你在找的jQuery相关文章