1

私はhtml5、js、css3を扱っています。

次のコードがあります。

HTML

<object id="bottom" data="img/model/skirt.svg"
title="bottom" type="image/svg+xml" width="325"
height="500"> </object>

JS

var b = document.getElementById("bottom");
b = b.getSVGDocument().getElementById("here");
alert(b);

、これにより「オブジェクト SVGPathElement」が得られます。

しかし、fill プロパティにアクセスしようとすると、「未定義」のメッセージが表示されます。 alert(b.fill);例えば。

fill プロパティは svg ファイルで確実に設定されています。私は明らかに何かが欠けています。任意のポインタをいただければ幸いです。

4

1 に答える 1

4

と を使用して SVG プロパティにアクセスする必要がsvgEl.setAttribute(name, value)ありsvgEl.getAttribute(name)ます。

のようなプロパティにアクセスするためのショートカットはありませんHTMLElement

ただし、本当に必要な場合は、次のように拡張できますSVGElement.prototype

Object.defineProperties(SVGElement.prototype, {
  fill: {
    get: function() {
      return this.getAttribute('fill');
    },
    set: function(value) {
      return this.setAttribute('fill', value);
    }
  }
});
于 2012-12-03T03:46:05.123 に答える