getElementById() is not working after modifying the SVG Document

a 夏天 提交于 2019-12-12 03:16:40

问题


I have a SVG document; say as below:

<svg>
   <g ...>
      <rect id="perm1" ../>
      <rect id="temp1" ../>
</svg>

in the run-time, I am changing the id of the second 'rect' from 'temp1' to 'calc_id1' using java script functions (see below); but immediately after modifying it, I am calling another function in which I am trying to retrieve the rect element using getElementById() with the new id 'calc_id1'; but it is returning null. I am not sure, what's wrong here but I can confirm that the rect element is updated with the new id. Any clue or answer will be of great help to me.

Please note that I am using IE9.

changeID( xmlDoc, "//g[@id='temp1']", "calc_id1");

function changeID( xmlDoc, xPath, newIdValue ) {
    var node = xmlDoc.selectSingleNode(xPath);
    if (node!=null){
            var oAttr = node.attributes.getNamedItem( "id");
            if (oAttr!=null){
                 oAttr.text = newIdValue;
            }
            return node;
    }
    else {
            return null;
    }
}

回答1:


It seems that getElementById() did not find the newly added elements because they were not recognized as svg element; they were missing the svg namespace in it. Once I added the namespace, it worked. Pleas see this answer for more details.



来源:https://stackoverflow.com/questions/9231367/getelementbyid-is-not-working-after-modifying-the-svg-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!