0

我想在 svg 中替换另一个渐变,所以我需要迭代(可能递归)每个 svg 项目以获得

我查看了 svg doc @ MDN https://developer.mozilla.org/fr/SVG/Element/ellipse 但它已损坏(访问被拒绝,没有示例,很多损坏的链接)

和 MSDN 上的完全不同 http://msdn.microsoft.com/en-us/library/ie/ff972071%28v=vs.85%29.aspx

所以我搜索有关如何访问 svg 元素的填充属性的任何指针,但是它已被定义。类似于 getComputedFill 函数

<svg 
 version="1.1"
   xmlns="http://www.w3.org/2000/svg" 
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:xlink="http://www.w3.org/1999/xlink"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"

 >
  <defs>
    <script>
        <![CDATA[

        function infothis(ref)
        {
        alert(ref + " " + ref.fill + " " + ref.fillOpacity); // why can't I get "url(#grad1)"  and 0.5 when clicking ellipse1
        //alert(ref.style.getPropertyValue("fill"));           // somewhat okwhy can't I get "url(#grad2)"  when clicking ellipse2

        }

        ]]>
    </script>
    <linearGradient 
    dc:title="red black filter" 
    dc:rights="CC-BY" 
    dc:description="basic filter ranging from black to red"
    id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" style="stop-color:rgb(0,0,0);stop-opacity:1" />
      <stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
    </linearGradient>

    <linearGradient id="grad2" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" style="stop-color:rgb(0,0,0);stop-opacity:1" />
      <stop offset="100%" style="stop-color:rgb(255,0,255);stop-opacity:1" />
    </linearGradient>

    <ellipse id="ellipse0" cx="150" cy="170" rx="85" ry="55" fill="url(#grad2)" />
  </defs>

  <ellipse id="ellipse1" cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" fill-opacity="0.5" onclick="infothis(this)" />
  <ellipse id="ellipse2" cx="100" cy="70" rx="85" ry="55" style="fill:url(#grad2)" onclick="infothis(this)" />
  <use  xlink:href="#ellipse0"/>
</svg>
4

1 回答 1

3

这个怎么样...

alert(document.defaultView.getComputedStyle(ref, null).getPropertyValue("fill"));
于 2012-06-24T19:06:44.320 回答