2016-10-11 101 views
1

Salut。 我已經證明與Firefox的IE瀏覽器。window.opener不同按鈕和輸入類型=按鈕

如果從按鈕調用paginaH1.html(功能openSon)(window.opener無法正常工作)。

如果你從input type = button調用工作。

如果單擊按鈕不起作用(A0-ObjectWindow,A1-未定義)。

如果單擊輸入類型= '按鈕' 工作(A0-ObjectWindow,A1-ObjectHTMLElement)。

這是頁面揭幕戰:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>prueba-Father</title> 
<script type="text/javascript"> 

function openSon() { 
window.datoPField = document.frmName.campoPadre; 
alert(window.datoPField+' ahora abro hijo'); 
a=window.open('paginaH1.html'); 
} 

</script> 
</head> 
<body> 
<form name="frmName"> 
<h1 id="text">Comunicacion entre dos paginas con Javascript.</h1> 
<input type="text" name="campoPadre" id="campoPadre" value="delPadre" > 
<input type="button" onClick="openSon()" value="input-button"> 
<button    onClick="openSon()">button</button> 
<button    onClick="window.datoPField = document.getElementById('campoPadre'); a=window.open('paginaH1.html');">bt+getElement</button> 
</form> 
</body> 
</html> 

現在pagninaH1.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Son page</title> 

<script type="text/javascript"> 
function iniciar() { 
alert(0); 
alert("a0-"+window.opener); 
alert("a1-"+window.opener.datoPField); 
alert("b0-"+window.opener.frmName.campoPadre); 
alert("c0-"+window.opener.datoPField.value); 
this.datoField = opener.datoPField; 
alert("d0-"+this.datoField); 
} 

</script> 

</head> 
<body ONLOAD="iniciar()"> 
<h1 id="text">esta es la pagina hijo </h1> 
<button onclick="this.window.close();">Cerrar</button> 
</body> 
</html> 

感謝。

回答

1

default typebutton元件是submit。即,<button>x</button><button type="submit">x</button>完全相同。當你在form(你這樣做)中有button時,點擊它在運行它的點擊處理程序後提交表單。 form沒有action的元素默認提交到頁面的URL,這會破壞當前頁面並用新副本(很容易錯過)替換它。

所以,當你點擊input,它運行它的點擊處理程序,開始打開彈出窗口的過程中,並沒有做任何事情。原始窗口,文檔和開啓者頁面的元素仍然存在。子窗口可以訪問這些元素。

但是,當您單擊button時,它將運行其單擊處理程序,啓動打開彈出窗口的過程,然後提交窗體,銷燬窗口,文檔和元素。子窗口不能訪問元素,它們不再存在。 (相反,元素存在,但孩子並沒有對它們的訪問。)

如果你想button有同樣的表現方式input確實,加type="button"它。 (是的,說<button type="button" ...>確實看起來很荒謬。:-))

+0

Thansk,非常感謝