我有一个正在显示的SVG文件,我想从中获取有关单击特定元素时的信息。

这是SVG的一部分:

        <g id="group327-2702" transform="translate(1539.96,-1043.15)" v:mID="327" v:groupContext="group">
        <v:custProps>
            <v:cp v:nameU="idApplication" v:lbl="idApplication" v:type="0" v:sortKey="1" v:langID="1036" v:val="VT4(143)"/>
            <v:cp v:nameU="labelFR" v:lbl="labelFR" v:type="0" v:sortKey="3" v:langID="1036" v:val="VT4(Carnet d&#39;ordres)"/>
            <v:cp v:nameU="labelEN" v:lbl="labelEN" v:type="0" v:sortKey="4" v:langID="1036"
                    v:val="VT4(Order Management System )"/>
            <v:cp v:nameU="type" v:lbl="Type" v:type="0" v:langID="1036" v:val="VT4(Business)"/>
            <v:cp v:nameU="appLevel" v:lbl="appLevel" v:type="0" v:langID="1036" v:val="VT4(3)"/>
            <v:cp v:nameU="name" v:lbl="name" v:type="0" v:sortKey="2" v:langID="1036" v:val="VT4(MCE Trading)"/>
            <v:cp v:nameU="External" v:lbl="External" v:type="0" v:langID="1036" v:val="VT4(FALSE)"/>
            <v:cp v:nameU="_VisDM_type" v:lbl="type" v:langID="1036" v:val="VT4(Business)"/>
            <v:cp v:nameU="_VisDM_status" v:lbl="status" v:type="2" v:langID="1036" v:val="VT0(1):26"/>
        </v:custProps>
        <v:userDefs>
            <v:ud v:nameU="msvStructureType" v:prompt="" v:val="VT4(Container)"/>
            <v:ud v:nameU="msvSDContainerMargin" v:prompt="" v:val="VT0(0.078740157480315):24"/>
            <v:ud v:nameU="Label" v:prompt="" v:val="VT0(2):26"/>
            <v:ud v:nameU="ShapeVersion" v:prompt="" v:val="VT0(1):26"/>
            <v:ud v:nameU="LightColorText" v:prompt="" v:val="VT0(0):5"/>
        </v:userDefs>
        <title>Module.52.327</title>
        <g id="shape328-2703" v:mID="328" v:groupContext="shape">
            <title>Feuille.328</title>
            <v:userDefs>
                <v:ud v:nameU="visVersion" v:val="VT0(14):26"/>
            </v:userDefs>
            <rect x="0" y="1652.6" width="93.5433" height="31.1811" rx="2.83465" ry="2.83465" class="st72"/>
        </g>
        <g id="shape327-2705" v:mID="327" v:groupContext="groupContent">
            <v:textBlock v:margins="rect(4,4,4,4)" v:tabSpace="42.5197"/>
            <v:textRect cx="46.7717" cy="1668.19" width="93.55" height="31.1811"/>
            <text x="28.54" y="1661.89" class="st73" v:langID="1036"><v:paragraph v:horizAlign="1"/><v:tabList/>MCE Trading<v:newlineChar/><v:newlineChar/><tspan
                        x="7.63" dy="2.4em" class="st5">Order Management System </tspan> </text>            </g>
    </g>


我需要访问节点<v:custProps>的第一个孩子的属性:

<v:cp v:nameU="idApplication" v:lbl="idApplication" v:type="0" v:sortKey="1" v:langID="1036" v:val="VT4(143)"/>

奇怪的是,每个节点之间都有空的文本同级:
例如,如果我这样做:

var msg = "";
    msg += "target nodeName, width x height : " + evt.target.nodeName + ", " + evt.target.getAttributeNodeNS(null, 'width').nodeValue + " x " + evt.target.getAttributeNodeNS(null, 'height').nodeValue + "\n\n";

    msg += "parent nodeName, id : " + evt.target.parentNode.nodeName + ", " + evt.target.parentNode.id + "\n\n";

    msg += "parent previousSibling nodeName : " + evt.target.parentNode.previousSibling.nodeName + "\n\n";

    msg += "parent 2 previousSibling nodeName : " + evt.target.parentNode.previousSibling.previousSibling.nodeName + "\n\n";

    msg += "parent 3 previousSibling nodeName : " + evt.target.parentNode.previousSibling.previousSibling.previousSibling.nodeName + "\n\n";

    msg += "parent 4 previousSibling nodeName : " + evt.target.parentNode.previousSibling.previousSibling.previousSibling.previousSibling.nodeName + "\n\n";

    msg += "parent 5 previousSibling nodeName : " + evt.target.parentNode.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.nodeName + "\n\n";

    msg += "parent 6 previousSibling nodeName : " + evt.target.parentNode.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.nodeName + "\n\n";

    msg += "parent 6 previousSibling firstChild nodeName : " + evt.target.parentNode.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.firstChild.nodeName + "\n\n";

    msg += "parent 6 previousSibling firstChild nextSibling nodeName : " + evt.target.parentNode.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.firstChild.nextSibling.nodeName + "\n\n";

alert(msg);


结果是:


  目标nodeName,宽度x高度:矩形,93.5433 x 31.1811
  
  父节点名称,ID:g,shape328-2703
  
  父previousSibling nodeName:#text
  
  父级2 previousSibling nodeName:标题
  
  父级3 previousSibling nodeName:#text
  
  父级4 previousSibling nodeName:v:userDefs
  
  父级5 previousSibling nodeName:#text
  
  父级6 previousSibling nodeName:v:custProps
  
  父级6 previousSibling firstChild nodeName:#text
  
  父级6 previousSibling firstChild nextSibling nodeName:v:cp


因此,现在我以一种奇怪的方式访问了我的节点<v:cp v:nameU="idApplication" v:lbl="idApplication" v:type="0" v:sortKey="1" v:langID="1036" v:val="VT4(143)"/>,我正在尝试获取属性。

例如 :
msg += "parent 6 previousSibling firstChild nextSibling v:nameU : " + evt.target.parentNode.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.previousSibling.firstChild.nextSibling.getAttributeNS(null, 'v:nameU') + "\n\n";

但这是行不通的。我尝试将id添加到行中,并且可以获取id属性。但是对于现有属性,它不起作用。

那么,为什么每个节点之间都没有出现在svg结构中的文本同级?

为什么我不能访问要获取的属性?是因为它们来自Visio(v :)

最佳答案

我认为您正在寻找nextElementSiblingpreviousElementSibling

TextNodes是节点,文档中标签之间的空格可能是TextNodes。



var g=document.querySelector('#group327-2702');
var cpElement = g.firstElementChild.firstElementChild.nextElementSibling;
var r = cpElement.getAttributeNS(null, "v:nameu")+' '+
    cpElement.getAttributeNS(null, "v:lbl")+' '+
    cpElement.getAttributeNS(null, "v:type")+' '+
    cpElement.getAttributeNS(null, "v:sortkey")+' '+
    cpElement.getAttributeNS(null, "v:langid")+' '+
    cpElement.getAttributeNS(null, "v:val");

document.querySelector('p').textContent= r;

<svg><g id="group327-2702" transform="translate(1539.96,-1043.15)" v:mID="327" v:groupContext="group"><v:custProps>
            <v:cp v:nameU="idApplication" v:lbl="idApplication" v:type="0" v:sortKey="1" v:langID="1036" v:val="VT4(143)"/>
            <v:cp v:nameU="labelFR" v:lbl="labelFR" v:type="0" v:sortKey="3" v:langID="1036" v:val="VT4(Carnet d&#39;ordres)"/>
            <v:cp v:nameU="labelEN" v:lbl="labelEN" v:type="0" v:sortKey="4" v:langID="1036"
                    v:val="VT4(Order Management System )"/>
            <v:cp v:nameU="type" v:lbl="Type" v:type="0" v:langID="1036" v:val="VT4(Business)"/>
            <v:cp v:nameU="appLevel" v:lbl="appLevel" v:type="0" v:langID="1036" v:val="VT4(3)"/>
            <v:cp v:nameU="name" v:lbl="name" v:type="0" v:sortKey="2" v:langID="1036" v:val="VT4(MCE Trading)"/>
            <v:cp v:nameU="External" v:lbl="External" v:type="0" v:langID="1036" v:val="VT4(FALSE)"/>
            <v:cp v:nameU="_VisDM_type" v:lbl="type" v:langID="1036" v:val="VT4(Business)"/>
            <v:cp v:nameU="_VisDM_status" v:lbl="status" v:type="2" v:langID="1036" v:val="VT0(1):26"/>
        </v:custProps>
        <v:userDefs>
            <v:ud v:nameU="msvStructureType" v:prompt="" v:val="VT4(Container)"/>
            <v:ud v:nameU="msvSDContainerMargin" v:prompt="" v:val="VT0(0.078740157480315):24"/>
            <v:ud v:nameU="Label" v:prompt="" v:val="VT0(2):26"/>
            <v:ud v:nameU="ShapeVersion" v:prompt="" v:val="VT0(1):26"/>
            <v:ud v:nameU="LightColorText" v:prompt="" v:val="VT0(0):5"/>
        </v:userDefs>
        <title>Module.52.327</title>
        <g id="shape328-2703" v:mID="328" v:groupContext="shape">
            <title>Feuille.328</title>
            <v:userDefs>
                <v:ud v:nameU="visVersion" v:val="VT0(14):26"/>
            </v:userDefs>
            <rect x="0" y="1652.6" width="93.5433" height="31.1811" rx="2.83465" ry="2.83465" class="st72"/>
        </g>
        <g id="shape327-2705" v:mID="327" v:groupContext="groupContent">
            <v:textBlock v:margins="rect(4,4,4,4)" v:tabSpace="42.5197"/>
            <v:textRect cx="46.7717" cy="1668.19" width="93.55" height="31.1811"/>
            <text x="28.54" y="1661.89" class="st73" v:langID="1036"><v:paragraph v:horizAlign="1"/><v:tabList/>MCE Trading<v:newlineChar/><v:newlineChar/><tspan
                        x="7.63" dy="2.4em" class="st5">Order Management System </tspan> </text>            </g>
    </g></svg>

<p id="log"></p>





另外,由于您的问题,我发现了一些东西,请注意,您必须使用getAttributeNS()小写要查找的属性

关于javascript - SVG节点的访问属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29754815/

10-13 09:13