簡體   English   中英

另一個窗口上的document.getElementById

[英]document.getElementById on another window

如果使用document.location.href="diffPage.htm?name="+string;啟動子窗口,則可以獲取前一個父窗口的ID document.location.href="diffPage.htm?name="+string; 字符串每次都會在哪里更改?

基本上,我的主窗口帶有id=favouritesTab ,稱為main.html,在調用函數時轉到diffPage.htm。

我已經嘗試過window.parent.document.getelementById()

var favouritesScreen = window.parent.document.getElementById('favouritesTab');
if(favouritesScreen.style.display == 'inherit')
{
    ..so on

但這似乎根本不起作用。 有誰知道是否有解決方案?

如果您的頁面托管在同一域中,則可以通過window.opener屬性訪問原始窗口中的文檔。

完整的演示在這里: http : //jsfiddle.net/eAjqX/1/show/

  1. 單擊按鈕打開一個新窗口。
  2. 在新窗口中單擊后,您將看到正文的內容更改為輸入字段的值。
  3. 使用原始頁面中提供的鏈接導航。
  4. 重復步驟2。您將看到window.opener.document.getElementById仍然有效。

該演示的代碼(不需要jQuery):

// New window page:
var i = 0;
document.onclick = function (){
    try {
        alert(window.opener.document.getElementById('test').value);
    } catch (e) {
        alert(e);
    }
};
// Launcher
window.open("http://jsfiddle.net/eAjqX/show");

暫無
暫無

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

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