0

SVG と JavaScript に問題があります。

このコードは期待どおりに動作します。

function initSvg(width) {
    SVGRoot = document.getElementsByTagName('svg')[0];
    console.log(SVGRoot.currentScale);    // Displays '1' which is OK

しかし、 currentScale パラメータを次のように再割り当てしようとすると:

function initSvg(width) {
    SVGRoot = document.getElementsByTagName('svg')[0];
    SVGRoot.currentScale = parseFloat(width)/400;   
    console.log(SVGRoot.currentScale);    // Should display some floating point number

エラーが発生します

Component returned failure code: 0x80070057 (NS_ERROR_ILLEGAL_VALUE) nsIDOMSVGSVGElement.currentScale]

アラートは実行されません。SVGRoot.currentScale への割り当ての何が問題になっていますか?

アップデート:

使用するとエラーが消えます

SVGRoot.setAttribute('currentScale', parseFloat(width)/400);

また

SVGRoot.setAttributeNS(null, 'currentScale', parseFloat(width)/400);

しかし、 currentScale の実際の値は変わりません。setAttribute に何が渡されても、1 のままです。

4

1 に答える 1

1

setAttribute(NS) の方法が正しくありません。SVG に「currentScale」属性がありません。

「currentScale」はSVGSVGElement インターフェイスのプロパティであり、数値が妥当である限り (たとえば、Inf や NaN ではない)、同じように取得および設定できるはずです。ただし、SVG 1.1 仕様では、最も外側の svg 要素でのcurrentScaleの動作のみが定義されていることに注意してください。

于 2012-05-24T09:05:40.157 に答える