MozTW 討論區 https://forum.moztw.org/ |
|
[解決] js 找多層次ifram的資料要如何寫?? https://forum.moztw.org/viewtopic.php?f=13&t=12519 |
第 1 頁 (共 1 頁) |
發表人: | david0928 [ 2006-01-21, 16:40 ] |
文章主題 : | [解決] js 找多層次ifram的資料要如何寫?? |
請問一下像下面這段要寫成firefox支援要如何寫 <iframe id="ifrm1"> <iframe id="ifrm1" src="aa.htm"></iframe> </iframe> <script language="javascript"> parent.ifrm1.ifrm1.document.location.href; </script> ps.因為第二層的iframe裡的url會變,所以用location.href |
發表人: | 風痕影 [ 2006-01-21, 17:14 ] |
文章主題 : | |
這個頁面的HTML本身就有問題 <iframe>應該不能套在<iframe>裡 而且ID不可以在同一個頁面上重複 |
發表人: | david0928 [ 2006-01-21, 17:26 ] |
文章主題 : | |
剛剛沒寫清楚,應該是這樣 aa.htm ============================ <script language="javascript"> alert(parent.ifrm1.ifrm1.document.location.href); </script> <iframe id="ifrm1" src="bb.htm"> </iframe> ============================ bb.htm ============================ <iframe id="ifrm1" src="cc.htm"></iframe> ============================ 然後要在aa.htm用js找bb.htm裡的ifram的url |
發表人: | 風痕影 [ 2006-01-22, 01:20 ] |
文章主題 : | |
欲表示某ID的元素,getElementById("某ID")是標準的寫法,所以: 代碼: document.getElementById("ifrm1").document.getElementById("ifrm1").document.location.href
不過有一點要注意,這個程式其實應該寫在<iframe>後面 否則執行時<iframe>還沒出現,其實是會出錯的喔 ^^ |
發表人: | david0928 [ 2006-01-22, 03:00 ] |
文章主題 : | |
這樣還是不行呢,因為這種寫法 代碼: document.getElementById("ifrm1").document.getElementById("ifrm1").document.location.href 跟這種是一樣的效果,只會偵測到最上一層的url(第一層) 代碼: document.all.ifrm1.document.all.ifrm1.document.location.href 必須這樣寫才能偵測到最底層的url,可是這樣寫FireFox又不支援 代碼: ifrm1.ifrm1.document.location.href 或 ifrm1.ifrm1.location.href
|
發表人: | yuoo2k [ 2006-01-22, 03:54 ] |
文章主題 : | |
代碼: <html>
<head> <script language="javascript"> function t() { alert( document.getElementById("ifrm1") .contentDocument.getElementById("ifrm1") .contentDocument.location.href ); } </script> </head> <body onload="t();"> <iframe id="ifrm1" src="bb.htm"/> </body> </html> |
發表人: | david0928 [ 2006-01-22, 09:59 ] |
文章主題 : | |
可以了:D 謝謝 yuoo2k 大和 風痕影 大~喔 代碼: <script language="JavaScript" type="text/JavaScript">
function check() { //記錄現在的網頁,以備登入時還在同一頁-開始// //for IE if (parent.document.getElementById("ifrm1").contentDocument == undefined){ document.getElementById("hldurl1").value = parent.ifrm1.document.location.href; if (parent.ifrm1.ifrm1 != undefined) document.getElementById("hldurl2").value = parent.ifrm1.ifrm1.document.location.href; //for FireFox }else{ document.getElementById("hldurl1").value =parent.document.getElementById("ifrm1") .contentDocument.location.href; if (parent.document.getElementById("ifrm1").contentDocument.getElementById("ifrm1") != undefined) document.getElementById("hldurl2").value = parent.document.getElementById("ifrm1") .contentDocument.getElementById("ifrm1") .contentDocument.location.href; } //記錄現在的網頁,以備登入時還在同一頁-結束// if (document.getElementById("account").value == "" ){ alert("請輸入帳號"); document.getElementById("account").focus(); return false; }else if(document.getElementById("password").value == "" ){ alert("請輸入密碼"); document.getElementById("password").focus(); return false; } return true; } </script> |
發表人: | kourge [ 2006-01-24, 14:24 ] |
文章主題 : | |
唉唷,用這麼多 document.getElementById,豈不是要累死?! 建議用這個 function... 代碼: function $(element){return document.getElementById(element);} 假如要 document.getElementById("password") 只要改用 $("password") 就好 如果要寫一個可以同時取得多個 element 的 function,可以這樣(從 Prototype 偷學來的技巧) 代碼: function $(){
var results = new Array(); for (i = 0; i < arguments.length; i++){ results.push(document.getElementById(arguments[i])); } return results; } |
第 1 頁 (共 1 頁) | 所有顯示的時間為 UTC + 8 小時 |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |