zoukankan      html  css  js  c++  java
  • 【转】jquery iframe取得元素与自适应高度

    今天没事来总结一下iframe在jquery中怎么操作的,下面我来给各位介绍jquery 获取iframe子/父页面的元素及iframe在jquery高度自适应实现方法,各位朋友可参考。

    jquery方法:

    在iframe子页面获取父页面元素

    代码如下:

    1 $('#objId', parent.document);

    在父页面 获取iframe子页面的元素
    代码如下:

    1 $("#objid",document.frames('iframename').document)
    2 
    3 $(document.getElementById('iframeId').contentWindow.document.body).html()

    显示iframe中body元素的内容。

    1 $("#testId", document.frames("iframename").document).html();

    根据iframename取得其中ID为"testId"元素。

    $(window.frames["iframeName"].document).find("#testId").html()

    在父窗口中操作 选中IFRAME中的所有输入框:

    $(window.frames["iframeSon"].document).find(":text");

    在IFRAME中操作 选中父窗口中的所有输入框:

    $(window.parent.document).find(":text");

    iframe框架的HTML:

    <iframe src="test.html" id="iframeSon" width="700″ height="300″ frameborder="0″ scrolling="auto"></iframe>

    在父窗口中操作 选中IFRAME中的所有单选钮。

    $(window.frames["iframe1"].document).find("input[@type='radio']").attr("checked","true");

    在IFRAME中操作 选中父窗口中的所有单选钮。

    $(window.parent.document).find("input[@type='radio']").attr("checked","true");

    iframe框架的:

    <iframe src="test.html" id="iframe1″ width="700″ height="300″ frameborder="0″ scrolling="auto"></iframe>

    假设有两个页面,在相同域下.

    index.html 文件内含有一个iframe:

    XML/HTML代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
    <title>页面首页</title> 
    </head> 
     
    <body> 
    <iframe src="iframe.html" id="koyoz" height="0" width="0"></iframe> 
    </body> 
    </html>  

    iframe.html 内容:

    XML/HTML代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
    <title>iframe.html</title> 
    </head> 
     
    <body> 
    <div id="test">www.</div> 
    </body> 
    </html> 

    iframe自适应高度:

    $("#iPersonalInfo").load(function() {
    
    $(this).height($(this).contents().height());
    
    })

    有一点需要注意的,我也在调试的时候才发现的,耽误了不少时间。就是绑定事件必须在iframe加载完毕之前绑定,否则不会执行。
    以下是jQuery,load事件的概述
    在每一个匹配元素的load事件中绑定一个处理函数。
    如果绑定给window对象,则会在所有内容加载后触发,包括窗口,框架,对象和图像。如果绑定在元素上,则当元素的内容加载完毕后触发。
    注意:只有当在这个元素完全加载完之前绑定load的处理函数,才会在他加载完后触发。如果之后再绑定就永远不会触发了。所以不要在$(document).ready()里绑定load事件,因为jQuery会在所有DOM加载完成后再绑定load事件。

    iframe代码,注意要写ID

    <iframe src="test.html" id="main" width="700" height="300" frameborder="0" scrolling="auto"></iframe>

    jquery代码1:

    //注意:下面的代码是放在test.html调用
    $(window.parent.document).find("#main").load(function(){
    var main = $(window.parent.document).find("#main");
    var thisheight = $(document).height()+30;
    main.height(thisheight);
    });

    jquery代码2:

    //注意:下面的代码是放在和iframe同一个页面调用
    $("#main").load(function(){
    var mainheight = $(this).contents().find("body").height()+30;
    $(this).height(mainheight);
    });
  • 相关阅读:
    HttpURLConnection请求网络数据的GET请求
    ImageLoader的Jar包加载图片
    使用HttpUtils 上传视频文件
    VideoView的视频播放
    VideoView的视频的全屏播放
    Android中在activity中弹出一个popwindow
    随心而记
    Java -- springboot 配置 freemarker
    Tensorflow替换静态图中的OP
    python 系统定时关机
  • 原文地址:https://www.cnblogs.com/PiaoMiaoGongZi/p/4220737.html
Copyright © 2011-2022 走看看