模仿QQ邮箱添加附件的javascript
来源:互联网 发布:身份证识别仪软件 编辑:程序博客网 时间:2024/04/29 08:21
<script type="text/javascript"><!--google_ad_client = "pub-4490194096475053";/* 内容页,300x250,第一屏 */google_ad_slot = "3685991503";google_ad_width = 300;google_ad_height = 250;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
贴出全部HTML+JS代码,在这里做个备份,当前版本暂定为1.0吧,以后还会改进。比如选择附件后,显示附件的大小。如果有bug或可以改进的地方,希望不吝赐教! <!--
Author: aqua qin
Tech blog:http://blog.csdn.net/aquadp/
Create Date: 2008-4-8 22:00:00
Last Modify Date: 2008-4-9 22:46:35
-->
<HTML>
<HEAD>
<script>
/**//*
需要注意的几个地方:
.由于input type=file控件不能对type属性赋值(in IE),所以采用插入HTML代码的方式(insertAdjacentHTML);
.input type=file控件的value是只读的,不能被赋值;
.因为用一个input type=file控件有缺陷,就是当用户删除了最后一次添加的文件再添加同样的文件则无法触发onchange事件;
并且要上传文件,只用一个上传控件是不够的;
.客户端无法获取文件大小,除非用FSO或ActiveX等。除了img控件,其fileSize可以在图片文件加载完成后获取其文件大小;
.还有另外一种变通的方法就是用flash与js交互达到获取客户端文件大小的效果(现在flash在安全方面也做了限制);
*/
var i = 0; // 用来动态生成span,upfile的id
function addAttachmentToList()
...{
if (findAttachment(event.srcElement.value)) return; //如果此文档已在附件列表中则不再添加
// 动态创建附件信息栏并添加到附件列表中
var span = document.createElement('span');
span.id = '_attachment' + i;
span.innerHTML = extractFileName(event.srcElement.value) + ' <a href="javascript:delAttachment(' + (i++) + ')"><font color="blue">删除</font></a><br/>';
span.title = event.srcElement.value;
G('attachmentList').appendChild(span);
// 显示附件列表并变换添加附件按钮文本
if (G('attachmentList').style.display == 'none')
...{
G('btnAdd').value = '继续添加';
G('attachmentList').style.display = '';
G('btnClear').style.display = '';
}
G('total').innerText = '当前选择上传'+ G('attachmentList').childNodes.length + '个附件';
}
function selectAttachment()
...{
// 先清除无效动态生成的多余upfile
cleanInvalidUpfile();
// 动态创建上传控件并与span对应
var upfile = '<input type="file" style="display:none" onchange="addAttachmentToList();" id="_upfile'+i+'">';
document.body.insertAdjacentHTML('beforeEnd', upfile);
G('_upfile'+i).click();
}
function extractFileName(fn)
...{
return fn.substr(fn.lastIndexOf('/')+1);
}
function findAttachment(fn)
...{
var o = G('attachmentList').getElementsByTagName('span');
for(var i=0;i<o.length;i++)
if (o[i].title == fn) return true;
return false;
}
function delAttachment(id)
...{
G('attachmentList').removeChild(G('_attachment'+id));
document.body.removeChild(G('_upfile'+id));
// 当附件列表为空则不显示并且变化添加附件按钮文本
if (G('attachmentList').childNodes.length == 0)
...{
G('btnAdd').value = '添加附件';
G('attachmentList').style.display = 'none';
G('btnClear').style.display = 'none';
}
G('total').innerText = '当前选择上传'+ G('attachmentList').childNodes.length + '个附件';
}
function cleanInvalidUpfile()
...{
var o = document.body.getElementsByTagName('input');
for(var i=o.length-1;i>=0;i--)
if (o[i].type == 'file' && o[i].id.indexOf('_upfile') == 0)
...{
if (!G('_attachment'+o[i].id.substr(7)))
document.body.removeChild(o[i]);
}
}
function clearAttachment()
...{
var o = G('attachmentList').childNodes;
for(var i=o.length-1;i>=0;i--)
G('attachmentList').removeChild(o[i]);
o = document.body.getElementsByTagName('input');
for(var i=o.length-1;i>=0;i--)
if (o[i].type == 'file' && o[i].id.indexOf('_upfile') == 0)
...{
document.body.removeChild(o[i]);
}
G('btnAdd').value = '添加附件';
G('attachmentList').style.display = 'none';
G('btnClear').style.display = 'none';
G('total').innerText = '当前选择上传0个附件';
}
function getAttachmentInfo()
...{
// 已知的js获取本地文件大小的三种方式
// 1.通过FSO 2.通过ActiveX 3.通过Flash(设置可能更麻烦)与js交互
// 注:QQ邮箱中获取本地文件大小就是采用第二种方式
}
function G(id)
...{
return document.getElementById(id);
}
</script>
</HEAD>
<BODY>
<fieldset style="border : 1px solid #84A24A;text-align:left;COLOR:#84A24A;FONT-SIZE:
12px;font-family: Verdana;padding:5px;">
<legend>模仿QQ邮箱添加附件</legend>
<input type="button" value="添加附件" id="btnAdd" onclick="selectAttachment();"> <input type="button" value="清空附件" id="btnClear" style="display:none" onclick="clearAttachment();">
<div id="attachmentList" style="margin:3px 0px 0px 0px;padding:4px 3px 4px 3px;background-color:#DEEBC6;display:none;border:1px solid #84A24A;">
</div>
<div id="total" style="margin:3px 0px 0px 0px;">当前选择上传0个附件</div>
</fieldset>
</BODY>
</HTML>
- 模仿QQ邮箱添加附件的javascript
- 模仿QQ邮箱添加附件的javascript
- 动态添加rows,模仿网易邮箱添加附件功能(自写)
- 腾讯QQ邮箱的"超大附件"功能, 很让人失望
- QQ邮箱今天大面积出现无法下载附件的问题
- python想要保存QQ邮箱富途的附件
- 批量上传不同图片(网易邮箱添加附件的应用)
- 今天学习的邮箱核心添加附件代码
- 类拟161邮箱批量添加附件的JS
- iphone 添加qq邮箱.
- 163邮箱连续上传附件的javascript代码
- qq邮箱附件文件名乱码处理方法
- python smtp QQ邮箱 发送附件
- 模仿QQ秀(javascript)
- 模仿QQ秀(javascript)
- 在iPhone6s系统邮箱添加QQ邮箱
- 网易邮箱添加附件功能原理浅析
- 【Python融于生活】之我想要保存QQ邮箱富途的附件
- Firefox - 附加组件 - 扩展 - Firebug - HTML面板 - 标记的显示也会与源代码不同,而是按照Firefox对文档结构的解析显示的
- 几种数据库连接方式优缺点比较------ODBC ADO DAO
- jquery和prototype冲突解决
- struts 中的用法
- Grub 4 DOS 简介
- 模仿QQ邮箱添加附件的javascript
- Java Ajax Framework ZK3.5 发布了!
- 一年中的十二个月(英语)分别表示什么意思/
- CSS+DIV+JS树形2层菜单,支持FF
- C#编程点滴之基础编程2:变量
- 正则判断 是否为中文
- Oracle数据库设计(定义约束 外键约束)(转)
- 使用Transact-SQL进行数据导入导出方法详解
- Grub 4 DOS新手指南