gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:23:02 24 4
gpt4 key购买 nike

我有内联 svg 并且想通过以下方式获取其中的一些元素:

svgdoc.getElementById( 'someid' );

适用于 Firefox、Chrome,但不适用于 Opera。结果是 null 。页面 (html5) 交付为 xhtml (content-type: application/xhtml+xml ) 和 html 根元素包含出现在 inline-svg (svg, inkscape, ...) 中的所有命名空间声明,该声明是使用 inkscape 创建并直接注入(inject)到源中的。

所以,html 元素看起来像这样:

<!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 源代码中删除。

编辑::

上面的解释有一个小错误... SVG 被注入(inject)到 ajax 响应的源中,而该源又通过 element.innerHTML 注入(inject)到 dom 中。请原谅这次失败!

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

console.log( svgdoc.getElementByID ) --> function getElementById(){ [native code] }

这实在是太奇怪了,到底是什么原因造成的呢?

最佳答案

我不确定这是否仍然与这个问题相关,但我最近发现如果要获取的元素是 SVG 子元素并且 SVG 还不是 DOM 的子元素,那么 Opera 12 无法 getElementById() 。

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

关于javascript - 内联 svg -- getElementById 在 Opera 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12277724/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com