0

我用于创建行并从数据库中获取值的代码:

   var response = JSON.parse(this.responseText);
    for(var ii=0; ii<response.length; ii++)
    {

        var road = document.createElementNS("http://www.w3.org/2000/svg", "line");
            road.x1.baseVal.value = response[ii].Linex1;         
            road.y1.baseVal.value = response[ii].Liney1;           
            road.x2.baseVal.value = response[ii].Linex2;           
            road.y2.baseVal.value = response[ii].Liney2;         
            road.setAttributeNS(null, "stroke", "#686968");  *
            road.setAttributeNS(null, "stroke-width", "14.520");
            road.setAttributeNS(null, "stroke-linecap", "round");
            road.setAttributeNS(null, "fill", "none");
            glblGeogSorc.getElementsByTagName("g")[0].appendChild(road);

     }

我的目标是删除与 x1、x2、y1 和 y2 对应的特定 svgline elem。

4

1 回答 1

1

You could give them an id attribute which is the string concatenation of x1 y1 x2 and y2 then document.getElementById("" + x1 + y1 + x2 + y2) would get them and document.removeChild would remove it.

Of course if x1, y1, x2 and y2 are slightly out it won't match. Is that an issue? If so you need to think about the rules you want to match by. One solution might be to round the x1, y1 ... to a certain number of decimal places as you put them into the id and round them the same way when you're looking up the element by its id.

于 2012-09-17T09:59:53.400 回答