【发布时间】:2013-05-15 13:29:27
【问题描述】:
我正在尝试调试原因
var mapWin = svgMapDoc.getElementById('Map');
离开mapWin 仍然是null。我设置了断点和监视,在我的 IDE 监视窗口中出现以下令人困惑的情况:
name: svgMapDoc.getElementById('MainSVG') value: null
neame: svgMapDoc.childNodes[2].attributes["id"].nodeValue value: "MainSVG"
(注意,'MainSVG' 是 'Map' 的父级。)
所以'svgMapDoc'中有id“MainSVG”的元素,它是第三个子节点,但getElementById没有返回它。为什么?我该如何解决?
=========== 编辑 ============
评论者要求提供 HTML。这很棘手,因为它非常复杂,但这里有一些片段。
它最初不是我的代码,而是来自英国国家统计局。他们提供对英国 2011 年全国人口普查数据的访问。他们提供的一种访问方法是通过 SOAP API,他们在 NeSS Data Exchange V2.0 页面上提供了如何使用他们的 API 的示例。我无法让一个示例正常工作。它位于 ZIP NDE Hub Client REST v2.0 (Zip) 37 Mb 中的 NDE v2.0 Excel Client 下,这是一个“基于 Excel 的集线器演示应用程序,允许用户建立已保存数据的存储库,并通过 SVG 专题图查看它”。请注意,我有made some changes to their sample。
地图<DIV> SVG 数据通过以下几行添加到页面中:
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>');
svgFile 变量设置为“la_12UB.svg”,该文件位于“边界”目录中。以下是“Boundaries/la_12UB.svg”的开始方式。
<?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"/>
将 SVG 从 DOM 放入“mapWin”的 JavaScript 代码是
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();
}
最后我遇到问题的代码是
function initialise() {
var mapWin = svgMapDoc.getElementById('Map');
fullExtent[0] = mapWin.getAttribute('viewBox').split(" ")[0];
【问题讨论】:
-
请发布您的 html
-
添加了一些代码片段。它由很多文件组成,但希望我已经包含了足够多的文件。 @RobertLongson - 我已经包含了 SVG 文件的前几行,从中您会看到没有设置命名空间。是这个问题吗?
-
@RobertLongson,你是明星。按照 Jonathan Watt 的 SVG Authoring Guidelines 的命名空间建议,我取出 DOCTYPE 内容并将 SVG 和 XLink 命名空间添加到根 SVG 元素。现在它起作用了!如果您有时间将您的评论作为答案发表,我会这样标记。
标签: javascript svg