簡體   English   中英

如何創建指向iframe當前位置的鏈接?

[英]How can I create a link that goes to an iframe's current location?

例如,如果我的iframe設置為google.com,則有人在iframe中進行搜索,然后將其加載到iframe中。 我想在iframe下有一個鏈接,上面寫着“轉到頁面”之類的內容。 當他們點擊它時,它不一定會帶他們到google.com,而是帶他們到iframe中當前所在的任何頁面。 我嘗試使用以下代碼無濟於事:

<iframe id=frame src="http://www.google.com" scrolling=yes></iframe>
<a id=moveOn href=# onclick='return false'>Click to go to page</a>
<script>
$('#moveOn').click(function(){
var src = document.getElementById('frame').contentDocument.location;
window.open(src);
});
</script>

有什么建議么? 謝謝 :)

在某些IE瀏覽器中,屬性contentDocument不存在。 下面的代碼應該更好,但是我並未在所有地方進行測試。

var src;
if(document.getElementById('frame').contentDocument)src = document.getElementById('frame').contentDocument.location;
else src = document.getElementById('frame').contentWindow.document.location

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM