小编Dra*_*mos的帖子

如何使用 JavaScript 选择 <object> 标签内的 SVG 元素?

在我的 Angular 应用程序中,我希望能够<object>使用 JavaScript 或 Angular jqLit​​e选择标记的嵌入 SVG 元素。

通常,要执行此操作,必须编写类似于以下内容的内容:

// Create <object> element of the SVG
var objElement = document.createElement('object');
objElement.setAttribute('type',"image/svg+xml");

// Assume $rootScope.b64 contains the base64 data of the SVG
objElement.setAttribute('data', $rootScope.b64);

// Append the <object> inside the DOM's body
angular.element(document.body).append(objElement);

console.log(objElement);
console.log(objElement.getSVGDocument());
console.log(objElement.contentDocument);
Run Code Online (Sandbox Code Playgroud)

在我的控制台中,objElement返回完整<object><svg>元素及其内容(假设 data 属性包含完整的 base64 数据字符串 (b64))。

    <object id="svgObject" data="b64" type="image/svg+xml">
          #document
             <svg>
             </svg>
    </object>
Run Code Online (Sandbox Code Playgroud)

然而,getSVGDocument()返回nullcontentDocument返回

    #document
       <html>
          <head></head>
          <body></body>
       <html> …
Run Code Online (Sandbox Code Playgroud)

html javascript svg cross-domain angularjs

5
推荐指数
2
解决办法
3336
查看次数

标签 统计

angularjs ×1

cross-domain ×1

html ×1

javascript ×1

svg ×1