SVG中异物的特征检测

问题描述 投票:3回答:2

我正在SVG中使用foreignObject元素,但是IE9不支持该元素。我正在寻找一种检测此功能的方法。 Modernizr未检测到此功能,似乎我无法像对矩形(createSVGRect)一样使用createSVGForeignObject(在SVGSVGElement上不可用)。

谢谢!

svg feature-detection
2个回答
4
投票

如果您要使用foreignObject,因为它集成了html内容,这应该可以工作...

<switch>
  <g requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility" requiredExtensions="http://www.w3.org/1999/xhtml">
    <foreignObject >
    </foreignObject>
  </g>
  <text font-size="10" font-family="Verdana">
     No foreignObject
  </text>
</switch>

[RequiredExtensions部分proposed to w3cthis was their response。 Firefox确实实现了这一点,但是我还没有测试其他任何东西。正如Erik所建议的,您也许可以只使用requiredFeatures属性。

如果您想使用JavaScript进行测试,请尝试

var supported = document.implementation.hasFeature("http://w3.org/TR/SVG11/feature#Extensibility", "1.1"); –  

2
投票

[有一种方法可以在JS中测试此功能,以下是从最近对modernizr(https://github.com/Modernizr/Modernizr/commit/ee836f083f29a9e634df731400027c24630a75f3)的提交中借来的:

        var toStringFnc = ({}).toString;
        Modernizr.addTest('svgforeignobject', function() {
            return !!document.createElementNS &&
                /SVGForeignObject/.test(toStringFnc.call(document.createElementNS('http://www.w3.org/2000/svg', 'foreignObject')));
        });
© www.soinside.com 2019 - 2024. All rights reserved.