2016-09-25 19 views
0

的index.html <object id="battle" data="battle.html"></object>的Html <object>,從外面

有一個動畫我想告訴讀的JavaScript檢測,但在動畫芬蘭我希望它關閉。 我試圖做的網頁消失,它的工作,但它後屏幕是無法點擊

battle.html document.getElementById("battlebody").style.display="none"; !The image!當動畫完成此代碼播放。基本上,我在它周圍做了一個邊框,告訴它在隱身的同時。

那麼我想知道是否有可能編輯index.html中的<object>標記,而不是像我所做的那樣在battle.html中編輯。

爲例 的index.html

<body> 
<object id="battle" data="battle.html"></object> 
</body> 

的battle.html

<body> 
    <button id="" onclick="closing(); ">close</button> 
    <script> 
    function closing() { 
    window.close() 
    </script> 
</body> 

我還挺想使物體接近,但它不工作的方式,從而使物體display:none;但爲此,您需要將該js代碼添加到index.html,但index.html不會讀入<object>

+0

做出小提琴PLS .. –

+0

@Bla ......不能因爲只有小提琴使用1個html頁面。 – Bamuel

+0

'object id =「battle」'...'document.getElementById(「battlebody」)。style.display =「none」;'......兩個不同的ID ......爲什麼?你有沒有試過'document.getElementById(「battle」)。style.display =「none」;' –

回答

2

您可以使用window.parent.postMessage方法。

目標頁面

<html> 
<body> 
    <script> 
    setTimeout(function() { 
     parent.postMessage('close-me', '*'); 
    }, 2000); 
    </script> 
</body> 
</html> 

的index.html

window.onmessage = function(e){ 
 
    if(e.data === 'close-me'){ 
 
    console.log("should remove"); 
 
    obj.parentNode.removeChild(obj); 
 
    } 
 
    };
object{border:1px solid}
<object id="obj" data="data:text/html; charset=utf8, %3Chtml%3E%3Cbody%3E%3Cscript%3EsetTimeout(function()%7Bparent.postMessage('close-me'%2C'*')%3B%7D%2C2000)%3B%3C%2Fscript%3E%3C%2Fbody%3E%3C%2Fhtml%3E"></object>

+0

謝謝,我會盡快試用 – Bamuel