iframe中用js或jQuery控制子页面

iframe父页面中利用javascript或jQuery控制子页面

近期的一个项目,希望修改iframe的页面中插入一段代码,但是牵扯到跨域问题无法实现,但是这种方法父页面控制子页面元素,子页面控制父页面元素的方法。

$(function(){

    $('#url_mainframe').attr("src", "http://www.baidu.com");

    $("iframe").contents().find("#div2").append("I'm in an iframe!");     
    
    $(document.getElementById('url_mainframe').contentWindow.document.body).find("#div2").html("添加内容x")
    
        document.write("框架个数:" + window.frames.length);

        document.write("框架个数:" + document.frames.length);//只支持ie      

    $("iframe").contents().find('#flashContent').append('<param name="wmode" value="transparent" />');

      
     });


document.getElementById('url_mainframe').contentWindow.document.getElementById('div2').style.color = 'red' //支持谷歌,火狐,不支持ie


从iframe中查找父页面中对象的方法:
  1.js
    
    [window.]parent //查找父页面的window对象  
    [window.]parent.document //查找父页面的document对象  
    [window.]parent.document.body //查找父页面的body对象  
    [window.]parent.document.getElementById('button') //查找父页面中id为button的对象  


  2.jquery

    $([window.]parent) //查找父页面的window对象  
    $([window.]parent.document) //查找父页面的document对象  
    $([window.]parent.document.body) //查找父页面的body对象  
    $([window.]parent.document.body).find('#button') //查找父页面中id为button的对象  

 

 

点击事件子页面控制父页面元素。
$('.demo_close').click(function () {
            $('#iframeIE', window.parent.document).hide();
  });   

 

 


从父页面中查找iframe子页面中对象的方法:
 1. JS代码:

  document.getElementById('iframe').contentWindow //查找iframe加载的页面的window对象  
  document.getElementById('iframe').contentWindow.document //查找iframe加载的页面的document对象  
  document.getElementById('iframe').contentWindow.document.body //查找iframe加载的页面的body对象  
  document.getElementById('iframe').contentWindow.document.getElementById('icontent') //查找iframe加载的页面的id为icontent的对象

 

 2.jQuery:
    $iframe.contents() //查找iframe加载的页面的document对象  
    $iframe.contents().find('body') //查找iframe加载的页面的body对象  
    $iframe.contents().find('body').find('#icontent') //查找iframe加载的页面的id为icontent的对象 

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇