2

I'm trying to debug why

var mapWin = svgMapDoc.getElementById('Map');

leaves mapWin still null. I have set up a breakpoint and a watch and have the following confusing situation in my IDE watch window:

name: svgMapDoc.getElementById('MainSVG') value: null
neame: svgMapDoc.childNodes[2].attributes["id"].nodeValue value: "MainSVG"

(N.B. 'MainSVG' is the parent of 'Map'.)

So the element with id "MainSVG" is there in 'svgMapDoc', it is the third child node, but getElementById fails to return it. Why? How can I fix it?

=========== EDIT ===========

Commenters are asking for the HTML. This is tricky as it is quite complex, but here are some fragments.

It's not originally my code but comes from the UK Office for National Statistics. They provide access to the data from the UK 2011 national census. One access method they provide is through a SOAP API and they give examples of how to consume their API on the page NeSS Data Exchange V2.0. I am having trouble getting one example working. It’s under NDE v2.0 Excel Client in the ZIP NDE Hub Client REST v2.0 (Zip) 37 Mb, a "hub demo Excel-based application which allows the user to build up a repository of saved data, and view it via an SVG thematic map". Note that I have however made some changes to their sample.

The map <DIV> SVG data is added to the page by these lines:

document.writeln('<DIV id=mapPanel style="position:absolute;left:26%;top:7%; height=63%; width=50%;">');
document.writeln('<EMBED height=100% width=100% name=svgMap onload="svgMapLoaded()" src="../Boundaries/' + svgFile + '" type=image/svg+xml>');
document.writeln('</DIV>');

The svgFile variable is set to "la_12UB.svg" and that file is present in the 'Boundaries' directory. Here is how 'Boundaries/la_12UB.svg' starts.

<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/css" href="external.css" ?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20000303 Stylable//EN" "http://www.w3.org/TR/2000/03/WD-SVG-20000303/DTD/svg-20000303-stylable.dtd">
<svg xml:space="preserve"  preserveAspectRatio="xMinYMin meet" id="MainSVG" width="395" height="420" viewBox="0 0 900 650"> 
  <g class="mapEdge" opacity="0.2">
      <rect x="0" y="0" width="100%" height="100%"/>            
  </g>
  <svg xml:space="preserve" preserveAspectRatio="xMinYMin meet" id="Map" onload="init(evt)" width="100%" height="100%" viewBox="541512 -262080 7829 10000">
      <g class="mapBackground" transform="translate(1000,500) scale(1, -1)">
          <path id="00JA" d="M 533246,308907 534698,301468 539690,302984 540167,303513 539729,302866 534716,301443 527492,299370 527194,299301 522976,298284 523340,298070 522788,297938 522896,297322 522287,296297 522752,296256 523134,295665 522992,295319 522142,295666 521979,295371 521209,295267 520981,294831 520598,295324 519801,295441 519692,294834 520037,294424 519608,293815 519307,293871 519182,293126 517207,292629 517375,292088 516523,291182 516301,291471 515933,291255 515710,292076 514043,294384 513155,295603 513611,295848 513756,296170 513511,296320 512826,296386 512500,296931 512035,297564 510765,297353 510349,297748 509529,297818 509347,298690 508718,299559 507903,299503 507472,299058 506810,299452 503855,298555 503186,298398 503176,298820 502294,299230 501879,299841 502341,300260 502671,301563 502968,301637 503035,302936 503435,302942 503614,303338 503418,304478 502550,305148 502370,304967 501950,305791 502644,306453 503260,306347 503212,306752 503764,306896 504753,306844 504914,307391 507122,306620 507966,306784 508919,307439 510829,308187 511058,308184 512364,308996 512669,309872 514367,309757 516476,308975 517551,307531 517895,307411 520168,309004 520976,309160 521409,309326 522343,307609 523190,308534 525850,307595 525946,307970 528419,309965 529405,309387 530284,310000 530904,310008 531006,310370 531399,310254 532300,309733 533178,309331 533246,308907 z"/>

The JavaScript code that puts the SVG from the DOM into 'mapWin' is

function svgMapLoaded() {
    if (document.svgMap && document.svgMap.contentDocument) {
        svgMapDoc = document.svgMap.contentDocument;
    } else try {
        svgMapDoc = document.svgMap.getSVGDocument();
    }
    catch (exception) {
        alert('Neither the HTMLObjectElement nor the GetSVGDocument interface are implemented');
    }
    initialiseIfReady();
}

And finally the code where I'm having trouble is

function initialise() {
    var mapWin = svgMapDoc.getElementById('Map');
    fullExtent[0] = mapWin.getAttribute('viewBox').split(" ")[0];
4

2 回答 2

4

如果您没有正确命名 SVG 文件,ID 通常不会按预期工作。

因此,解决方案是向xmlns="http://www.w3.org/2000/svg"<svg>元素添加一个属性以及所需的任何其他名称空间声明,例如 xlink。

于 2013-05-15T15:22:39.693 回答
3

因为EMBED不会使其嵌入的 ID 可见。将embed其视为页面中的一个洞,外部进程在其中呈现某些内容。从浏览器的角度来看,内容是完全不透明的。通常,它用于浏览器插件支持的视频或奇怪的图像格式 - 浏览器确实不假设该元素内部可能包含什么。

可以遵循以下建议:如何在 <img> 元素中访问 SVG 文件的内容?

但这仅在元素的命名空间正确时才有效:

<svg xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     ...>
</svg>

没有它,它可能是一个自定义元素,恰好具有svg浏览器不会触及的名称。

有关详细信息,请参阅HTML中的 SVG。

于 2013-05-15T15:17:22.617 回答