首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >内联svg -- getElementById在Opera中不起作用

内联svg -- getElementById在Opera中不起作用
EN

Stack Overflow用户
提问于 2012-09-05 16:51:56
回答 1查看 288关注 0票数 3

我有内联svg,并希望通过以下方式获得它的一些元素:

代码语言:javascript
复制
svgdoc.getElementById( 'someid' );

适用于Firefox、chrome,但不适用于Opera。结果就是null。页面(html5)作为xhtml (内容类型: application/xhtml+xml )交付,html根元素包含inline-svg (svg,inkscape,... )中出现的所有名称空间声明。它是用inkscape创建的,直接注入到源代码中。

因此,html-element如下所示:

代码语言:javascript
复制
<!DOCTYPE html>
<html 
    xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:svg="http://www.w3.org/2000/svg"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:cc="http://creativecommons.org/ns#"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
    xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape">

其中包含的svg头部如下所示:

代码语言:javascript
复制
<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   version="1.1"
   width="581.75085"
   height="382"
   inkscape:version="0.48.3.1 r9886" >

<?xml ... ?>声明在html-source中被删除。

编辑::

上面的解释中有一个小错误...SVG被注入到ajax响应的源代码中,然后通过element.innerHTML注入到dom中。请原谅这次失败!

顺便说一句:getElementsByTagName()是有效的。

代码语言:javascript
复制
console.log( svgdoc.getElementByID ) --> function getElementById(){ [native code] }

这真的很奇怪,是什么导致了这种情况呢?

EN

回答 1

Stack Overflow用户

发布于 2017-03-09 08:09:34

我不确定这是否仍然与这个问题相关,但我最近发现,如果要获取的元素是SVG子元素,并且SVG还不是DOM的子元素,则Opera12不能使用getElementById()。

因此,在使用新创建的SVG中的ID对元素调用document.getElementById()之前,我们必须将整个SVG添加到DOM中,方法是将其附加到document.body或中的其他容器中。然后它就起作用了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12277724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档