如何获取Object Tag的ParentElement? [英] How to get the ParentElement of Object Tag?
问题描述
我有一个通过对象标签嵌入的SVG图形。
I have a SVG graphic embedded via object tag.
<!DOCTYPE html>
<html>
<head>
<title>myTitle</title>
<script type="text/javascript" src="script.js"></script>
<link href="box.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div id ="objectcontainer">
<div id="displaybox" style="display: none;"></div>
<object id = "mainSVG" type="image/svg+xml" data="map_complete.svg">
<img id="svgPic" src="map_complete.svg" alt="Browser fail"/>
</object>
</div>
</body>
</html>
在SVG中是一个链接:
In the SVG is a link:
<a id="emerBtn" xlink:href="emergency.html" onmouseover="return playVideo()" target="_parent">
鼠标悬停事件应触发以下内容:
The mouse over event should trigger the following:
function playVideo(){
//not working, all the time null
var doc = document.parentNode;
var elem = document.parentElement;
var otherElem = document.documentElement.parentElement;
//working if triggered from index.html
var thediv = document.getElementById('displaybox');
if(wasViewed == false) //show only one time
{
if(thediv.style.display == "none"){
wasViewed = true;
thediv.style.display = "";
thediv.innerHTML = "<div id='videocontainer'><video autoplay controls style='display:block; margin-left:auto;" +
"margin-right:auto; margin-top:150px; margin-bottom:auto; width:600px'>" +
"<source src='video.mp4' type='video/mp4'>HMTL5-Video not supported!</video>" +
"</div><a href='#' onclick='return palyVideo();'>CLOSE WINDOW</a>";
}else{
thediv.style.display = "none";
thediv.innerHTML = '';
}
} //close anyhow
else{
thediv.style.display = "none";
thediv.innerHTML = '';
}
return false;
}
我的问题是,我无法从svg访问displaybox。
我试过.parentNode,.parentElement,document.documentElement.parentElement等。
但是父元素/节点一直是null。
My problem is, that i cannot access the "displaybox" from the svg. I tried .parentNode, .parentElement, document.documentElement.parentElement etc. But all the time the parent element/node is null.
有谁知道如何从object / svg访问外部HTML元素?
Does anyone know how to access the "outer" HTML elements from the object/svg?
推荐答案
<$ c内的SVG $ c> object 创建嵌套浏览上下文。
要访问此子文档之外的元素,您需要访问父文档:
To access elements outside this child document, you need to access the parent document:
function playVideo() {
// ...
var parentDoc = window.parent.document;
var displayBox = parentDoc.getElementById('displaybox');
// ...
}
这篇关于如何获取Object Tag的ParentElement?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!