请参阅此示例。
svg 中的代码如下所示:
document.getElementById("svgroot").addEventListener("click",
sendClickToParentDocument,
false);
function sendClickToParentDocument(evt)
{
// SVGElementInstance objects aren't normal DOM nodes,
// so fetch the corresponding 'use' element instead
var target = evt.target;
if(target.correspondingUseElement)
target = target.correspondingUseElement;
// call a method in the parent document if it exists
if (window.parent.svgElementClicked)
window.parent.svgElementClicked(target);
else
alert("You clicked '" + target.id + "' which is a " + target.nodeName + " element");
}
在父文档中,您有一个带有要调用的函数的脚本,例如:
function svgElementClicked(theElement)
{
var s = document.getElementById("status");
s.textContent = "A <" + theElement.nodeName +
"> element with id '" + theElement.id +
"' was clicked inside the <" +
theElement.ownerDocument.defaultView.frameElement.nodeName.toLowerCase() +
"> element.";
}