zoukankan      html  css  js  c++  java
  • iframe里访问父级里的方法属性

    1. window.parent.attributeName;  // 访问属性attributeName是在父级窗口的属性名  
    2. window.parent.Func();  // 访问属性Func()是在父级窗口的方法 

      window可以不写

    项目里用于上传文件功能使用了iframe,在引用iframe的页面里取父类元素或JS里的变量时候,需加上window.parent。

    在index.JS定义的JS变量bootStrapFileUploadUrl = “上传的url”;在iframe里取的时候如下:

    parent.bootStrapFileUploadUrl 

    给父类html里的img设置值,如下:

    parent.document.getElementById("hotel_photo").

      setAttribute("src",parent.window.bootStrapFileUploadUrl+"/upload/uploadHotel/"+response.statusMsg);

    parent.closeUpFileDiv();//关闭图片上传组件

    引用页面代码:

    <%@ page language="java" contentType="text/html; charset=utf-8"
        pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>酒店基本信息-->文件上传</title>
    </head>
    <body>
        <link href="${pageContext.request.contextPath }/css/bootstrap.min.css" media="all" rel="stylesheet" type="text/css" />
        <link href="${pageContext.request.contextPath }/css/fileinput.min.css" media="all" rel="stylesheet" type="text/css" />
        
        
        <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-3.1.1.min.js" ></script>
        <!-- bootstrap文件上传 js引用文件-->
        <script src="${pageContext.request.contextPath }/js/bootstrap.min.js" type="text/javascript"></script>
        <script src="${pageContext.request.contextPath }/js/bsupload/fileinput.min.js" type="text/javascript"></script>
        <script src="${pageContext.request.contextPath }/js/bsupload/es.js" type="text/javascript"></script>
        <script src="${pageContext.request.contextPath }/js/bsupload/zh.js" type="text/javascript"></script>
    
            <input id="file-0a" name="file" class="file" type="file" multiple data-min-file-count="1"/>
            <script>
        $("#file-0a").fileinput({
            allowedFileExtensions : [ 'jpg', 'png', 'gif' ],
            showUpload : true,
            showRemove : false,
            language : 'zh',
            allowedPreviewTypes : [ 'image' ],
            allowedFileTypes : [ 'image' ],
            maxFileCount : 1,
            maxFileSize : 2000,
    
            minImageWidth : 50, //图片的最小宽度
            minImageHeight : 50,//图片的最小高度
            maxImageWidth : 480,//图片的最大宽度
            maxImageHeight : 480//图片的最大高度
            ,uploadUrl :'../upfile/uploadHotelFile'
            //bstrapupload'
    
        });
        $('#file-0a').on('fileuploaded',function(event, data, previewId, index) {
            var form = data.form, 
            files = data.files,
            extra = data.extra,
            response = data.response,
            reader = data.reader;
            parent.window.document.getElementById("hotel_photo").setAttribute("src",parent.window.bootStrapFileUploadUrl+"/upload/uploadHotel/"+ response.statusMsg);
            parent.window.closeUpFileDiv();//关闭图片上传组件
        });
        
    </script>
    </body>
    </html>
    第一种情况:iframe中不存在name和id的方法:(通过contentWindow获取)
    var iframe = document.getElementsByTagName('iframe')[0];
    var ifr_document = iframe.contentWindow.document;//iframe中的文档内容
    或者:
    var _iframe = document.getElementByIdx_x('iframeId').contentWindow;  
    var _div =_iframe.document.getElementByIdx_x('objId');  
    或者:
    1. var frameWin=document.getElementById('iframe').contentWindow;    //window对象  
    2. var frameDoc=document.getElementById('iframeId').contentWindow.document  //document对象  
    3. var frameBody=document.getElementById('iframeId').contentWindow.document.body   //body对
    第二种情况:iframe中存在name或者id的方法:(通过frames[]数组获取)
    document.frames['iframe的name'].document.getElementById('元素的ID');
    第三种情况:在iframe中获取父级页面的id元素 :
    var obj=window.parent.document.getElementById('objId') ;
    $(window.parent.document).find("#objId").css('height':'height);   // window可省略不写 jquery
     
    第四种情况:父级窗体访问iframe中的属性
    1. a、 用contentWindow方法   
    2. document.getElementById('iframe1').onload=function(){  
    3.          this.contentWindow.run();  
    4.  }  
    5. b、用iframes[]框架集数组方法  
    6. document.getElementById('iframe1').onload=function(){  
    7.          frames["iframe1"].run();  
    第五种情况:在iframe中访问父级窗体的方法和属性 //window 可以不写
    1. window.parent.attributeName;  // 访问属性attributeName是在父级窗口的属性名  
    2. window.parent.Func();  // 访问属性Func()是在父级窗口的方法 
     
    第五种情况:让iframe自适应高度
      1. $('#iframeId').load(function() { //方法1  
      2.     var iframeHeight = Math.min(iframe.contentWindow.window.document.documentElement.scrollHeight, iframe.contentWindow.window.document.body.scrollHeight);  
      3.     var h=$(this).contents().height();  
      4.     $(this).height(h+'px');   
      5. });  
      6.   
      7. $('#iframeId').load(function() { //方法2  
      8.     var iframeHeight=$(this).contents().height();  
      9.     $(this).height(iframeHeight+'px');   
      10. }); 
  • 相关阅读:
    全排列算法(java实现) 组合算法实现
    int -2147483648 ----- 2147483647
    Gradel 多渠道打包 代码混淆
    Android开发环境搭建
    自定义相册、九宫格显示图片
    新技术:retrofit
    新技术:dagger
    主流架构:MVVP
    GreenDao3.2的使用
    第三方框架:EventBus
  • 原文地址:https://www.cnblogs.com/ryans/p/6677726.html
Copyright © 2011-2022 走看看