在iframe父界面獲取iframe里面的標簽


在iframe父界面獲取iframe里面的標簽

上一篇里邊介紹了在里邊利用iframe引入另一個html導航文件,有興趣的朋友可以看一看 http://www.cnblogs.com/simba-lkj/p/6031662.html

目前遇到一些問題們,想要在引用了iframe的文件里修改iframe里邊的樣式。

解決了這個問題,整理一下,供大家參考

demo:

<div class="iframe">

<iframe src="header.html" width="100%" height="442px" marginwidth="0" frameborder="no" scrolling="no"></iframe>

</div>

1、這是我首先考慮的一個方法

獲取iframe引入的html文件元素通過id獲取

要在網頁加載完之后再進行這段代碼 
window.onload = function(){
 var test = document.getElementById('iframe的id').contentWindow.document.getElementById('iframe里要獲取的元素的id');
 console.log(test);
 test.style.display = "none";
}
 
2、可以在iframe文件js里獲取父網頁的相對路徑,將相對路徑存儲在一個數組中,通過判斷相對路徑 來進行操作

實例:

var URL  window.parent.location.pathname;

路徑數組:

var arrURL =["/CASE/aishuoke/index.html","/CASE/aishuoke/about.html","/CASE/aishuoke/news.html","/CASE/aishuoke/download.html","/CASE/aishuoke/case.html","/CASE/aishuoke/job.html","/CASE/aishuoke/shownews.html",

"/CASE/aishuoke/showinfo.html","/CASE/aishuoke/showimg_first.html","/CASE/aishuoke/showimg_second.html","/CASE/aishuoke/showimg_third.html","/CASE/aishuoke/showimg_forth.html"];

 

switch (URL){

case arrURL[0]:

$("#shouye").addClass("show").siblings().removeClass("show");

break;

}

 

總結:

在iframe中獲取父窗口的元素

$('#父窗口中的元素ID', parent.document).click();jquery

在父窗口中獲取iframe中的元素 

1、:var test = $("#iframe的ID").contents().find("#iframe中的控件ID");//jquery 方法1 

  var test2 =  $("#iframe中的控件ID",document.frames("frame的name").document);//jquery 方法2

  var test3 = document.getElementById('iframe的id').contentWindow.document.getElementById('iframe里要獲取的元素的id');//js方法
 
 轉載請注明出處,謝謝合作*-* http://www.cnblogs.com/simba-lkj/p/6032458.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM