-1

次のindex.htmlファイルがあります。

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
$( document ).ready(function() {
    $("#inline").click(function (event) { $(event.target).attr('style', "fill: yellow"); })
    $("#abc").click(function (event) { $(event.target).attr('style', "fill: yellow"); })
    $("#def").click(function (event) { $(event.target).attr('style', "fill: yellow"); })
    $("#circle2").click(function (event) { $(event.target).attr('style', "fill: yellow"); })
});
</script>
</head>
<body>

<svg id="inline" height="100" width="100" xmlns="http://www.w3.org/2000/svg">
  <circle cx="50" cy="50" r="25" stroke="black" stroke-width="3" fill="blue" />
</svg>

<svg id="abc" height="100" width="100" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
    <image id="def" x="0" y="0" height="500" width="500" xlink:href="red.svg" />
</svg>

</body>
</html>

およびファイルred.svg:

<svg id="circle2" xmlns="http://www.w3.org/2000/svg">
  <circle cx="50" cy="50" r="25" stroke="black" stroke-width="3" fill="red" />
</svg>

ブラウザでは、index.html ファイルに 2 つの円が表示されます。青と赤。大丈夫です。

クリックした後に円の色を変更したい。「インライン」の青い円で機能します。しかし、挿入された svg (赤) では何も起こりません。

インポートした画像に色を付けるにはどうすればよいですか?

または、svg ファイルをインポートする他の方法はありますか? URLからファイルをインポートする必要があるため、jQuery.load()を使用できませんfile://

4

1 に答える 1

0

CSS スタイルは、境界を越えて機能しません。#def にスタイル プロパティを設定できますが、red.svg の内容には継承されません。

次のいずれかが必要です。

(a) インライン red.svg、または

(b) DOM 経由でコンテンツにアクセスできるように埋め込みます (経由my_object.contentDocument)

于 2014-12-20T05:30:39.903 に答える