如何获取Object Tag的ParentElement?

时间:2015-04-24 08:12:32

标签: javascript html object

我有一个通过对象标签嵌入的SVG图形。

<!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中有一个链接:

<a id="emerBtn" xlink:href="emergency.html" onmouseover="return playVideo()" target="_parent">

鼠标悬停事件应触发以下内容:

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;
}

我的问题是,我无法访问&#34; displaybox&#34;来自svg。 我试过.parentNode,.parentElement,document.documentElement.parentElement等。 但是父元素/节点一直是空的。

有谁知道如何访问&#34;外部&#34;来自object / svg的HTML元素?

1 个答案:

答案 0 :(得分:0)

object内的SVG会创建nested browsing context

要访问此子文档外部的元素,您需要访问父文档:

function playVideo() {
  // ...
  var parentDoc = window.parent.document;
  var displayBox = parentDoc.getElementById('displaybox');
  // ...
}