问题空间:
我有一个 SVG map ,我使用 JQUERY Ajax 嵌入到 html 中。然后我使用 java 脚本来处理 svg 对象中的许多元素,以更改属性并添加基于数据系列的事件监听器。数据和 map 是交互式的。用户可以通过许多排列来改变状态。
我也在使用 JQUERY UI 1.10.3 Tabs
有很多类别。因此,根据演示文稿,同一个选项卡上可能存在 svg map 的多个副本,并且许多不同的选项卡具有相同的 svg map 。
svg 文档的每个副本都具有相同的元素 ID。我遇到的问题是元素 ID 在 HTML DOM 中相互冲突。因此唯一可寻址的元素是 svg 对象中最先出现在 HTML 文档中的元素。
我的问题是:
是否需要为 svg 对象的每个实例设置唯一的元素 ID?或者有没有办法为 HTML DOM 中的 svg 对象设置范围和上下文?
示例:
每个包含的 div 都有一个唯一的 ID,我用它来插入 svg 文档和检索 SVG 文档。
<div id="svgMap_div_1" class="cIG" style="height: 500px; width: 750px"></div>
用于加载从 ajax 接收到的 svg 文档的代码(mimeType = "image/svg+xml"):
document.getElementById('svgMap_div_1').appendChild(svgXMLObject.documentElement)
用于检索 svg 文档的代码。 svgDoc 的声明对于实现它的函数是局部的,并且是唯一的。
var svgDoc = document.getElementById('svgMap_div_1').ownerDocument;
用于更新单个元素的代码。当相同的 svg 对象(子元素具有相同的 ID)在同一个 HTML DOM 中时,此 javascript 将始终寻址 HTML DOM 中最先出现的 svg 对象,即使它是从离散的 div 中检索的,例如 id=' svgMap_div_2'.
var svgElem = svgDoc.getElementById('elemID');
svgElem.setAttribute("fill", '#0000FF');
我认为问题的根源在于 svgDoc 是使用 ownerDocument 检索的。它不是创建具有作用域和上下文的离散对象,而是遍及整个 DOM,因此 DOM 就是上下文。从 IDE 中查看代码引用:
HTMLDocument Node.ownerDocument
Property ownerDocument http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/ecma-script-binding.html
See Also:
Document
Since:
Standard ECMA-262 3rd. Edition
Level 2 Document Object Model Core Definition.
@type
Document
如果必须的话,我可以开发一个可行的算法来使所有 ID 都独一无二,但我确实想找到更好的方法。我已经尝试过其他方法和属性来获得一个有效的 svg 文档对象,但这些都不适合我。
下面是一个工作示例,用于演示当两个 svg 对象位于同一页面上的不同 div 中时会发生什么。所有四种组合都会改变第一个矩形的颜色。如果您将第二个对象中矩形的 ID 更改为“box-b”并相应地对其进行寻址(“bbox_div”、“box-b”),那么两个矩形将相互独立地运行。
<script type='text/javascript'>
function colorBox(divID, svgElem, colorHex) {
/* DOM This is the original solution that did not work !!!*/
// var svgDoc = document.getElementById(divID).ownerDocument.
// var svgBox = svgDoc.getElementById(svgElem);
// svgBox.setAttribute("fill", colorHex);
/* querySelector */
// document.querySelector("#"+divID + " ."+svgElem).setAttribute("fill", colorHex);
/* jquery div */
// var svgBox = $('#'+divID+' svg').find('#'+svgElem);
// svgBox.css("fill", colorHex);
/* jquery class */
$('#'+divID+' svg').find('.'+svgElem).css("fill", colorHex);
}
</script>
<div style="height: 100px; width: 100px">
<a href="#" onclick="colorBox('abox_div', 'box-a', '#FF0000');return false;">Box A Red</a><br>
<a href="#" onclick="colorBox('abox_div', 'box-a', '#0000FF');return false;">Box A Blue</a><br>
<a href="#" onclick="colorBox('bbox_div', 'box-a', '#FF0000');return false;">Box B Red</a><br>
<a href="#" onclick="colorBox('bbox_div', 'box-a', '#0000FF');return false;">Box B Blue</a><br -->
</div>
<div id="abox_div" style="height: 100px; width: 100px">
<svg xmlns="http://www.w3.org/2000/svg"
width="0.990919in"
height="0.597218in"
viewBox="0 0 71.3461 42.9997">
<style type="text/css">
<![CDATA[
.allboxes {fill:#00FF00}
]]>
</style>
<g class="box-a allboxes" transform="translate(0.24,-0.24)" fill="#e8eef7">
<rect x="0" y="0.48" width="70.8661" height="42.5197" ><title>Box A</title></rect>
</g>
</svg>
</div>
<div id="bbox_div" style="height: 100px; width: 100px">
<svg xmlns="http://www.w3.org/2000/svg"
width="0.990919in"
height="0.597218in"
viewBox="0 0 71.3461 42.9997">
<g class="box-a allboxes" transform="translate(0.24,-0.24)" fill="#e8eef7">
<rect x="0" y="0.48" width="70.8661" height="42.5197" ><title>Box B</title></rect>
</g>
</svg>
</div>
最佳答案
如果您有重复的 ID,我认为严格来说是无效的 HTML。我建议使用其他一些识别元素的机制,使用类而不是 ID 或 data-
属性。然后你可以从 .getElemntById()
移动到 .querySelector()
( try on JS Bin ):
<html>
<head>
<title></title>
</head>
<body>
<div id="div1">
<svg xmlns="http://www.w3.org/2000/svg" width="50px" height="50px">
<rect class="rect1" width="50px" height="50px"/>
</svg>
</div>
<div id="div2">
<svg xmlns="http://www.w3.org/2000/svg" width="50px" height="50px">
<rect class="rect1" width="50px" height="50px"/>
</svg>
</div>
<script type="text/javascript">
document.querySelector("#div1 .rect1").setAttribute("fill","red");
document.querySelector("#div2 .rect1").setAttribute("fill","green");
</script>
</body>
</html>
或 .getElementsByClassName()
( try on JS Bin ):
<html>
<head>
<title></title>
</head>
<body>
<div id="div1">
<svg xmlns="http://www.w3.org/2000/svg" width="50px" height="50px">
<rect class="rect1" width="50px" height="50px"/>
</svg>
</div>
<div id="div2">
<svg xmlns="http://www.w3.org/2000/svg" width="50px" height="50px">
<rect class="rect1" width="50px" height="50px"/>
</svg>
</div>
<script type="text/javascript">
document.getElementById("div1").getElementsByClassName("rect1")[0].setAttribute("fill","red");
document.getElementById("div2").getElementsByClassName("rect1")[0].setAttribute("fill","green");
</script>
</body>
</html>
如果您将 ID 属性保持原样,.querySelector()
可能也会起作用,例如:
document.querySelector("#div1 [id=rect1]")
但我仍然建议使用 data-
属性,因为这可以避免任何潜在的 ID 陷阱。
关于javascript - .getElementById 的内联 SVG 和 HTML DOM 范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18649277/