svgタグをjavascriptで変更してサイズを変更しようとしていますが、変更しても効果がありません。これを行う必要がある理由は、このタグが変更できないライブラリによってレンダリングされるためです。したがって、私の唯一の選択肢は、javascriptを使用してsvgを変更することであるように思われます。
このスクリプトが生成するタグは正しいことを知っています。タグを新しいhtmlドキュメントにコピーでき、それが機能するため(サンプルコードに含めました)、強制する方法が必要なようです。 svgは、変更されたことを認識します(またはsvgを変更する別の方法)。
これが私の問題を示すHTMLページです:
<html>
<head>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var svg = $('#testsvg').find('svg')[0];
var w = svg.getAttribute('width').replace('px', '');
var h = svg.getAttribute('height').replace('px', '');
svg.removeAttribute('width');
svg.removeAttribute('height');
svg.setAttribute('viewbox', '0 0 ' + w + ' ' + h);
svg.setAttribute('preserveaspectratio', 'xminymin meet')
$(svg)
.css('width', '100%')
.css('height', '100%')
.css('background-color', 'white');
});
</script>
</head>
<body style="background-color: #333;">
<div style="width: 80%; height: 40%;">
<svg id="resultsvg" xmlns="http://www.w3.org/2000/svg" version="1.1" style="width: 100%; height: 100%; background-color: white; " viewbox="0 0 100 100" preserveaspectratio="xminymin meet">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="2" fill="red"></circle>
</svg>
</div>
<div id="testsvg" style="width: 80%; height: 40%;">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="100px" height="100px">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="2" fill="red" />
</svg>
</div>
</body>
</html>