前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >js通过contentWindow控制iframe子页面元素点击事件,并把值传给父页面[通俗易懂]

js通过contentWindow控制iframe子页面元素点击事件,并把值传给父页面[通俗易懂]

作者头像
全栈程序员站长
发布2022-11-03 10:21:08
8.5K0
发布2022-11-03 10:21:08
举报
文章被收录于专栏:全栈程序员必看

本来需要点击一个图片后,显示一个iframe上传框.点击上传,从而操作子页面中的点击上传动作,再把值传给父页面.或控制父页面中iframe元素的显示状态.不过.通过upload()函数,可以不用显示上传框了,直接激活子页面中的上传动作.另外,onchange事件则可以自动提交上传,不必用户点击上传按钮了.三步并做一步

代码语言:javascript
复制
<!-- 父页面中的上传按钮 -->
<img src="ui/images/myimg.png" class="link" onclick="upload()" />

<iframe src="upload.php"id="box_paint_container" ></iframe>



upload.php 文件中有一个表单



<form enctype="multipart/form-data" action="upload.php"id="forms" method="post" name="upform" style="position: relative; ">  
  <input name="upfile" type="file"id="file" onchange="document.getElementById('forms').submit();
  window.parent.document.getElementById('box_paint_container').style.display='none';;"> 
</form>  

下面是父页面中的一个函数
<script type="text/javascript">

function upload(){
var a = document.getElementById('box_paint_container').contentWindow.document.getElementById("file");
a.click();

}
</script>

upload.php中还有一个php上传功能,兼容手机版,可上传单张图片. 一并分享下

代码语言:javascript
复制
//上传文件类型列表 
require_once 'config.php';
require_once '../system/config/database.inc.php';
//require_once '../system/funcs/global.fun.php';
require_once 'incl/main.inc';
dbconnect(); $settings=get_settings(0); $options=get_options(); $lang=get_language(); 
if(isset($_COOKIE['uid'])){
$uid=intval(_encrypt(_getcookie("uid"),'DECODE'));	
$shopid=$_COOKIE['shopid'];	
$query='SELECT * FROM '.$dbss['prfx']."_member WHERE uid=$uid";
$result=neutral_query($query);
if(neutral_num_rows($result)>0){
$ext_user=neutral_fetch_array($result);
$user=array();
$uname=$ext_user['username'];
$heading=$ext_user['headimg']==''?'/statics/uploads/'.$ext_user['img']:$ext_user['headimg'];
if($uname==''){
$uname=substr_replace($ext_user['mobile'],"****",5,2);
}
}
}
if(!isset($uid)){
//权限
redirect('/index.php/mobile/user/login.php');die();
}
$uptypes=array(  
'image/jpg',  
'image/jpeg',  
'image/png',  
'image/pjpeg',  
'image/gif',  
'image/x-png'  
);  
$max_file_size=2000000;     //上传文件大小限制, 单位BYTE  
$destination_folder="uploadimg/"; //上传文件路径  
$watermark=0;      //是否附加水印(1为加水印,其他为不加水印);  
$watertype=1;      //水印类型(1为文字,2为图片)  
$waterposition=1;     //水印位置(1为左下角,2为右下角,3为左上角,4为右上角,5为居中);  
$waterstring="http://www.xplore.cn/";  //水印字符串  
$waterimg="xplore.gif";    //水印图片  
$imgpreview=0;      //是否生成预览图(1为生成,其他为不生成);  
$imgpreviewsize=1/2;    //缩略图比例  
if ($_SERVER['REQUEST_METHOD'] == 'POST')  
{
if (!is_uploaded_file($_FILES["upfile"]['tmp_name']))  
//是否存在文件  
{  
echo "图片不存在!";  
exit;  
}  
$file = $_FILES["upfile"];  
if($max_file_size < $file["size"])  
//检查文件大小  
{  
echo "文件太大!";  
exit;  
}  
if(!in_array($file["type"], $uptypes))  
//检查文件类型  
{  
echo "文件类型不符!".$file["type"];  
exit;  
}  
if(!file_exists($destination_folder))  
{  
mkdir($destination_folder);  //上传到的位置
}  
$filename=$file["tmp_name"];  
$image_size = getimagesize($filename);  
$pinfo=pathinfo($file["name"]);  
$ftype=$pinfo['extension'];  
$destination = $destination_folder.time().".".$ftype;  
if (file_exists($destination) && $overwrite != true)  
{  
echo "同名文件已经存在了";  
exit;  
}  
if(!move_uploaded_file ($filename, $destination))  
{  
echo "移动文件出错";  
exit;  
}  
$pinfo=pathinfo($destination);  //文件名
$fname=$pinfo['basename']; 
$line='<img onclick="imgbig(this)"src="'.$destination_folder.$fname.'"  style="width:100px;" />';
$query='INSERT INTO '.$dbss['prfx']."_lines VALUES(NULL,$uid,'$uname',$timestamp,'$line','$shopid','$heading','',0)";
neutral_query($query);
//print_r($line);exit;
//echo " <font color=red>上传中....</font><br>";  
// echo " 宽度:".$image_size[0];  
// echo " 长度:".$image_size[1];  
// echo "<br> 大小:".$file["size"]." bytes";  
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/180125.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月19日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档