简体   繁体   English

Snap.svg更改颜色属性

[英]Snap.svg changing colour attr

I am trying to learn how to use snap.svg and I am currently trying to change the colour of a part of my svg. 我正在尝试学习如何使用snap.svg,目前正在尝试更改svg的一部分颜色。 However everytime I try this the page goes blank and i get the error (Uncaught TypeError: Cannot read property 'attr' of null) This is my 但是,每次我尝试此操作时页面都会空白,并且出现错误(未捕获的TypeError:无法读取null的属性'attr')

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Test</title>
    <script src="dist/snap.svg.js"></script>
</head>
<body>
<script>
    var s = Snap(800,600);
    var g = s.group();
    var cube = Snap.load("theCube2.0.svg", function ( f ) {

        var g = f.select("SVGID_2_");
        g.attr({fill: '#101010'});
        s.append( f );
    } );


</script>

I have also linked my svg. 我还链接了我的svg。 https://dl.dropboxusercontent.com/u/185556553/theCube2.0.svg https://dl.dropboxusercontent.com/u/185556553/theCube2.0.svg

You forgot to mention the css selector #SVGID_2_ not SVGID_2_ 您忘记提及css选择器#SVGID_2_而不是SVGID_2_

Use 采用

var g = f.select("#SVGID_2_"); 

syntax: 句法:

f.selector("css selector")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM