【发布时间】:2013-05-30 13:45:31
【问题描述】:
我有一个 SVG(十字),它根据使用 JavaScript 提供给 SVG url 的哈希值来改变线条的颜色。
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<line x1="0" y1="0" x2="100%" y2="100%" stroke="black" stroke-width="1" />
<line x1="100%" y1="0" x2="0" y2="100%" stroke="black" stroke-width="1" />
<script>
if (window.location.hash) {
document.getElementsByTagName('line')[0].setAttribute('stroke', location.hash);
document.getElementsByTagName('line')[1].setAttribute('stroke', location.hash);
}
</script>
</svg>
这作为 <object> 元素 (<object type="image/svg+xml" data="img/svg/cross.svg#ff0000"></object>) 非常有效,但作为 img 或 css background-image 失败。
我怎样才能使它作为 CSS background-image 工作?
【问题讨论】:
-
你不能,SVG 用作背景图像,因此根据规范是非动态的 - 它只是一个静态图像。
-
@WladimirPalant — 有趣……哪个规范?我原以为它会说什么 here 但我看不到它。
-
@Quentin:在规范中找不到它,所以它似乎只是一个合乎逻辑的安全限制 - 请参阅我的答案。
-
SVG 1.1 规范的作者之一创建了这个...schepers.cc/svg/blendups/embedding.html
标签: javascript css svg