我有内联svg,并希望通过以下方式获得它的一些元素:
svgdoc.getElementById( 'someid' );适用于Firefox、chrome,但不适用于Opera。结果就是null。页面(html5)作为xhtml (内容类型: application/xhtml+xml )交付,html根元素包含inline-svg (svg,inkscape,... )中出现的所有名称空间声明。它是用inkscape创建的,直接注入到源代码中。
因此,html-element如下所示:
<!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头部如下所示:
<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()是有效的。
console.log( svgdoc.getElementByID ) --> function getElementById(){ [native code] }这真的很奇怪,是什么导致了这种情况呢?
发布于 2017-03-09 08:09:34
我不确定这是否仍然与这个问题相关,但我最近发现,如果要获取的元素是SVG子元素,并且SVG还不是DOM的子元素,则Opera12不能使用getElementById()。
因此,在使用新创建的SVG中的ID对元素调用document.getElementById()之前,我们必须将整个SVG添加到DOM中,方法是将其附加到document.body或中的其他容器中。然后它就起作用了。
https://stackoverflow.com/questions/12277724
复制相似问题