2015-02-24 112 views
0

我不明白。我以爲我是這樣做的,現在我做不到了。 簡單的HTML與iframe。顯示iframe是另一個html。我想在訪問iframe時從主iframe中更改圖像。從主HTML HTML:無法從iframe訪問父元素

主HTML:

<!DOCTYPE HTML> 
<html lang="en"> 
    <head></head> 
    <body> 
     <img id="logo" src="../img/fpdfilms-logo-home.png" /> 
     <iframe id="fondo" name="main" src="test.html"></iframe> 
    </body> 
</html> 

的test.html

<!DOCTYPE HTML> 
<html lang="en"> 
    <head></head> 
    <body> 
     <a href="#" onclick="change();">Change Picture</p> 
     <script src="../js/jquery-1.11.2.min.js"></script> 
     <script type="text/javascript"> 
      function change() 
      { 
       var $logo= $('#logo', window.parent.document); 
       alert("Does it work?"); 
       $logo.attr("src","../img/fpdfilms-logo-director.png"); 
      } 
     </script> 
    </body> 
</html> 

它應該改變圖片,當你點擊鏈接。但警報沒有顯示......我嘗試過不同的方式,我真的迷路了。實現這個簡單的改變是如此困難?

+0

可能的重複[如何在jQuery中編寫此代碼] window.parent.document.getElementById('parentPrice')。innerHTML「?](http://stackoverflow.com/questions/726816/how-to-write-這在jquery窗口父母文件getelementbyidparentprice) – 2015-02-24 08:49:50

回答

0

從一個iframe中選擇一個元素經過你的頁面(鉻40),並得到了

SecurityError: Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match. 

所以我檢查在服務器(本地快遞)和工作正常。

+0

...謝謝你,但你能指導我如何解決這個問題嗎? – nanmaniac 2015-02-24 15:22:06

+0

我想通了。我正在檢查本地PC中的文件。在服務器中它是不同的!有用 !!!! – nanmaniac 2015-02-24 15:41:47

1

你可以參考這個答案,因爲它做你所需要的jQuery。

how to access iFrame parent page using jquery?

從上面的例子在父文件使用

var $parentElement= $('#parentElement', window.parent.document); 
+0

我編輯了htmls爲了成爲最簡單的代碼。它不工作......任何其他想法? – nanmaniac 2015-02-24 13:12:09