1

我尝试获取嵌入在 .svg 中的元素<object>。我使用 svgweb 并强制使用 Flash 查看器。我创建了一个小演示 html 文档:

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <script src="svg.js"></script>
  <meta name="svg.render.forceflash" content="true">
 </head>
 <body>
   <!--[if !IE]>-->
    <object data="simple.svg" type="image/svg+xml"
        width="500" height="500" id="testSVG"> <!--<![endif]-->
   <!--[if lt IE 9]>
   <object src="simple.svg" classid="image/svg+xml"
        width="500" height="500" id="testSVG"> <![endif]-->
   <!--[if gte IE 9]>
   <object data="simple.svg" type="image/svg+xml"
        width="500" height="500" id="testSVG"> <![endif]-->
</object>

flash 渲染<object>器用<div id="testSVG" style="visibility: hidden;" type="image/svg+xml" width="500" height="500" data="simple.svg">.

svg 非常简单:

 <svg  xmlns="http://www.w3.org/2000/svg"
      xmlns:xlink="http://www.w3.org/1999/xlink">
  <rect id="rect" x="10" y="10" height="100" width="100"
      style="stroke:#ff0000; fill: #0000ff"/>
 </svg>

现在,我尝试<rect>根据手册(http://svgweb.googlecode.com/svn/trunk/docs/UserManual.html#manipulate_objects)获取 -element:

var obj = document.getElementById('testSVG');
var doc = obj.contentDocument; // grab the document object inside your SVG file
var myRect = doc.getElementById('myRect');

ie9 告诉我该对象不支持属性或方法“getElementById”。

我也在chrome上试过这个。代替<div>上面的,flash 渲染器生成一个<embed src="/svg.swf" quality="high" wmode="transparent" width="500" height="500" id="testSVG" name="testSVG" swliveconnect="true" allowscriptaccess="always" type="application/x-shockwave-flash" flashvars="uniqueId=testSVG&amp;sourceType=string&amp;clipMode=both&amp;debug=true&amp;svgId=testSVG" pluginspage="http://www.macromedia.com/go/getflashplayer" style="visibility: visible; " class="embedssvg">.

doc.getElementById('myRect')确实返回一个对象。在 Firefox 中,obj.contentDocument返回undefined.

我不关心 chrome 和 ff,因为它们有足够的原生 svg 支持。但我确实需要 ie9 才能让我在 svg 中添加元素 - 我该怎么做;我究竟做错了什么?

4

0 回答 0