首页 »Javascript教程 » javascriptdhtml:Javascript & DHTML 例子编程(教程)( 3)初级例子篇1—上传文件Control控件例子 »正文javascriptdhtml:Javascript & DHTML 例子编程(教程)( 3)初级例子篇1—上传文件Control控件例子来源: 发布时间:星期六, 2008年12月27日 浏览:15次 评论:0
效果DEMO:
http://www.never-online.net/tutorial/js/upload/ Javascript & DHTML 例子编程(教程)( 3) ![]() 上章基本上把要交代 ![]() ![]() ![]() 首先来做 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 也许对于单单看前几章 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 如果还有不懂 ![]() ![]() ![]() 首先看 ![]() ![]() ![]() ![]() ![]() ![]() ![]() 1、构造 ![]() ![]() ![]() ![]() ![]() ![]() ![]() 2、必须有 ![]() ![]() ![]() ![]() 3、必须有 ![]() ![]() ![]() ![]() 2)、这个类中应该有 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 1、得到 ![]() ![]() 2、 ![]() ![]() ![]() ![]() 整个图可以简单 ![]() ![]() 2、我想我们该想想应该用到哪些知识 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 2)、既然是Control控件 ![]() ![]() ![]() ![]() ![]() ![]() 3)、如何组织呢?在上面 ![]() 接下来就动手写: ![]() ![]() ![]() ![]() <script> function upload(target/*容器*/ ) { this._cnt = 0; /*计数器*/ this.target = document.getElementById(target); }; upload.prototype.add = function ![]() /* *生成 ![]() *生成 ![]() *生成 ![]() *计数器+1 */ }; upload.prototype.remove = function ![]() /* *删除 ![]() *删除 ![]() *删除 ![]() */ }; </script> 2、写出add思路方法 ![]() <script> upload.prototype.add = function ![]() /* *生成 ![]() */ var self = this; var cnt = this._cnt; var cFile = document.createElement("input"); cFile.type="file"; cFile.name="upload"; cFile.id = "upload_file_" +cnt; /* *生成 ![]() */ var cAdd = document.createElement("span"); cAdd.innerHTML="添加"; cAdd.onclick = function ![]() self.add ![]() }; /* *生成 ![]() */ var cRemove = document.createElement("span"); cRemove.innerHTML="删除"; cRemove.onclick = function ![]() self.remove(cnt); }; cAdd.id = "upload_add_" +cnt; cRemove.id = "upload_remove_" +cnt; /* 把所有生成 ![]() this.target.appendChild(cFile); this.target.appendChild(cAdd); this.target.appendChild(cRemove); /* 计数器+1 */ this._cnt ![]() ![]() }; </script> 3、写出remove思路方法 ![]() <script> upload.prototype.remove = function (n) { /* *删除 ![]() */ var a = document.getElementById("upload_file_" +n); a.parentNode.removeChild(a); /* *删除 ![]() */ var a = document.getElementById("upload_add_" +n); a.parentNode.removeChild(a); /* *删除 ![]() */ var a = document.getElementById("upload_remove_" +n); a.parentNode.removeChild(a); ![]() } </script> 上面remove思路方法过于重复 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() <script> upload.prototype._removeNode = function (id) { var a=document.getElementById(id); a.parentNode.removeChild(a); }; upload.prototype.remove = function (n) { /* *删除 ![]() */ this._removeNode("upload_file_" +n); /* *删除 ![]() */ this._removeNode("upload_add_" +n); /* *删除 ![]() */ this._removeNode("upload_remove_" +n); ![]() } </script> 4、将代码组合 ![]() ![]() <script> function upload(target/*容器*/ ) { this._cnt = 0; /*计数器*/ this.target = document.getElementById(target); }; upload.prototype.add = function ![]() /* *生成 ![]() */ var self = this; var cnt = this._cnt; var cFile = document.createElement("input"); cFile.type="file"; cFile.name="upload"; cFile.id = "upload_file_" +cnt; /* *生成 ![]() */ var cAdd = document.createElement("span"); cAdd.innerHTML="添加"; cAdd.onclick = function ![]() self.add ![]() }; /* *生成 ![]() */ var cRemove = document.createElement("span"); cRemove.innerHTML="删除"; cRemove.onclick = function ![]() self.remove(cnt); }; cAdd.id = "upload_add_" +cnt; cRemove.id = "upload_remove_" +cnt; /* 把所有生成 ![]() this.target.appendChild(cFile); this.target.appendChild(cAdd); this.target.appendChild(cRemove); /* 计数器+1 */ this._cnt ![]() ![]() }; upload.prototype._removeNode = function (id) { var a=document.getElementById(id); a.parentNode.removeChild(a); }; upload.prototype.remove = function (n) { /* *删除 ![]() */ this._removeNode("upload_file_" +n); /* *删除 ![]() */ this._removeNode("upload_add_" +n); /* *删除 ![]() */ this._removeNode("upload_remove_" +n); ![]() } </script> 5、OK ![]() ![]() <html> <head> <script> //这里是上面我们写 ![]() ![]() ![]() </script> </head> <body> <div id="uploadContainer"></div> <script> var o= ![]() o.add ![]() </script> </body> </html> 6、嗯 ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() 1、加 ![]() 2、每添加 ![]() ![]() ...等 我们这里添加 ![]() ![]() ![]() ![]() ![]() <script> upload.prototype.add = function ![]() /* *生成 ![]() */ var self = this; var cnt = this._cnt; var cWrap = document.createElement("div"); cWrap.id = "upload_wrap_" +cnt; var cFile = document.createElement("input"); cFile.type="file"; cFile.name="upload"; cFile.id = "upload_file_" +cnt; /* *生成 ![]() */ var cAdd = document.createElement("span"); cAdd.innerHTML="添加"; cAdd.onclick = function ![]() self.add ![]() }; /* *生成 ![]() */ var cRemove = document.createElement("span"); cRemove.innerHTML="删除"; cRemove.onclick = function ![]() self.remove(cnt); }; cAdd.id = "upload_add_" +cnt; cRemove.id = "upload_remove_" +cnt; /* 把所有生成 ![]() cWrap.appendChild(cFile); cWrap.appendChild(cAdd); cWrap.appendChild(cRemove); this.target.appendChild(cWrap); /* 计数器+1 */ this._cnt ![]() ![]() }; </script> 7、加上CSS美化 ![]() ![]() ![]() <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> upload control - http://www.never-online.net </title> <style type="text/css" media="all" title="Default"> * { font-family:Arial; } body { font-size:10pt; } h1 { } #footer { font-size:9pt; margin:20px; } span { margin: 3px; text-decoration:underline; cursor:default; } </style> <script type="text/javascript"> //<![CDATA[ function upload(target) { this._cnt = 0; this.target = document.getElementById(target); }; upload.prototype.add = function ![]() var self = this; var cnt = this._cnt; var cWrap = document.createElement("div"); cWrap.id = "upload_wrap_" +cnt; var cFile = document.createElement("input"); cFile.type="file"; cFile.name="upload"; cFile.id = "upload_file_" +cnt; var cAdd = document.createElement("span"); cAdd.innerHTML="添加"; cAdd.onclick = function ![]() self.add ![]() }; var cRemove = document.createElement("span"); cRemove.innerHTML="删除"; cRemove.onclick = function ![]() self.remove(cnt); }; cAdd.id = "upload_add_" +cnt; cRemove.id = "upload_remove_" +cnt; cWrap.appendChild(cFile); cWrap.appendChild(cAdd); cWrap.appendChild(cRemove); this.target.appendChild(cWrap); this._cnt ![]() ![]() }; upload.prototype._removeNode = function (id) { var a=document.getElementById(id); a.parentNode.removeChild(a); }; upload.prototype.remove = function (n) { this._removeNode("upload_file_" +n); this._removeNode("upload_add_" +n); this._removeNode("upload_remove_" +n); ![]() }; _disibledevent="footer">tutorial of DHTML and javascript programming, Power By never-online.net</div> </body> </html> 0
相关文章
读者评论发表评论 |
|