效果DEMO:
http://www.never-online.net/tutorial/js/upload/
Javascript & DHTML 实例编程(教程)(三),初级实例篇—上传文件控件实例
上章基本上把要交代的基本知识都说了一些,今天终于开始写代码了:D
首先来做一个实例,批量上传的UI控件。以后一般做的示例也是以UI控件为主的。都是封装成Object或者用Function封装成"Class"类。
也许对于单单看前几章的朋友来说这个例子过于深奥了,但是不用担心,一步步来解释应该很快理解的,关键是理解怎么做,而不是怎么写。
如果还有不懂的朋友,可以留言给我。
首先看一个成品截图预览:
一、接下来我们先说思路,首先定义一个upload"类",
一)、这个类的公共访问信息应该有:
1、构造函数中要传递一些必要的参数,比如,在哪个容器构造upload的信息。
2、必须有一个add()方法,用于添加一个upload
3、必须有一个remove()方法,用于删除一个upload
二)、这个类中应该有一些必要的信息,是生成实例本身所具有的信息,(upload对象的一些信息)。
1、得到一共多少个upload信息,
2、一个容器对象,这个对象也是从构造函数中传递。
整个图可以简单的表示为
2. I think we should think about what knowledge should be used, which ones are familiar and which ones are unknown.
1). As we can see in the preview above, three or more new controls are required. (Add, delete, there is a file control, or there may be others...but at least that’s what the eyes can see), since it is new information, document.createElement may be used, and it needs to be added. The object.appendChild(obj) or obj.insertBefore() method may be used in a container. Deletion is obj.parentNode.removeChild(obj). All this has been said in the previous chapter.
2). Since it is a control, it must be encapsulated with a function or an object. This part of the knowledge has been briefly explained in Chapter 1.
3) How What about organizations? There are already text and illustrations in the above ideas
Next, let’s start writing:
1), constructor, and basic code (pseudocode)
<script> <br>function upload(target/*container*/ <br> ) target); <br>}; <br><br>upload.prototype.add = function () { <br> /* <br> * Generate a file <br> * Generate an add <br> * Generate a delete <br> *Counter 1 <br> */ <br>}; <br><br>upload.prototype.remove = function () { <br> /* <br> *Delete a file <br> *Delete a Add <br> *Delete one Delete <br> */ <br>}; <br></script>
2. Write the implementation of the add method
<script> <br>upload.prototype.add = function () { <br> /* <br> *Generate a file <br> */ <br> var self = this; var cnt = this._cnt; <br> var cFile = document.createElement("input"); <br> cFile.type="file"; cFile.name="upload"; <br> cFile.id = "upload_file_" cnt; <br> /* <br> * Generate an Add<br> */ <br> var cAdd = document.createElement("span"); <br> cAdd.innerHTML="Add"; <br> cAdd.onclick = function () { <br> self. add(); <br> }; <br> /* <br> *Generate a delete<br> */ <br> var cRemove = document.createElement("span"); <br> cRemove.innerHTML="Delete "; <br> cRemove.onclick = function () { <br> self.remove(cnt); <br> }; <br><br> cAdd.id = "upload_add_" cnt; <br> cRemove.id = "upload_remove_" cnt; <br><br> /* Add all generated information to the container */ <br> this.target.appendChild(cFile); <br> this.target.appendChild(cAdd); <br> this.target.appendChild(cRemove); <br><br> /* counter 1 */ <br> this._cnt; <br><br> return this; //return <br>}; <br>< ;/script> <br><br>3. Write the implementation of the remove method <br><br><script> <br>upload.prototype.remove = function (n) { <br> /* <br> *Delete a file <br> */ <br> var a = document.getElementById("upload_file_" n); <br> a.parentNode.removeChild(a); <br> /* <br> *Delete a Add<br> */ <br> var a = document.getElementById("upload_add_" n); <br> a.parentNode.removeChild(a); <br> /* <br> *Delete one Delete <br> */ <br> var a = document.getElementById("upload_remove_" n); <br> a.parentNode.removeChild(a); <br><br> return this; <br>} <br></script>
The above remove method is too repetitive. Can we consider re-simplifying the remove method to make our code shorter and easier to maintain?在这里,我们把这个通用功能放到一个函数里,也就是多加一个函数:
<script> <br>upload.prototype._removeNode = function (id) { <br> var a=document.getElementById(id); <br> a.parentNode.removeChild(a); <br>}; <br><br>upload.prototype.remove = function (n) { <br> /* <br> *删除一个 file <br> */ <br> this._removeNode("upload_file_" +n); <br> /* <br> *删除一个 添加 <br> */ <br> this._removeNode("upload_add_" +n); <br> /* <br> *删除一个 删除 <br> */ <br> this._removeNode("upload_remove_" +n); <br><br> return this; <br>} <br></script>
四、将代码组合一下,基本上可以算是完成了:D
<script> <br>function upload(target/*容器*/ <br> ) <br>{ <br> this._cnt = 0; /*计数器*/ <br> this.target = document.getElementById(target); <br>}; <br><br>upload.prototype.add = function () { <br> /* <br> *生成一个 file <br> */ <br> var self = this; var cnt = this._cnt; <br> var cFile = document.createElement("input"); <br> cFile.type="file"; cFile.name="upload"; <br> cFile.id = "upload_file_" +cnt; <br> /* <br> *生成一个 添加 <br> */ <br> var cAdd = document.createElement("span"); <br> cAdd.innerHTML="添加"; <br> cAdd.onclick = function () { <br> self.add(); <br> }; <br> /* <br> *生成一个 删除 <br> */ <br> var cRemove = document.createElement("span"); <br> cRemove.innerHTML="删除"; <br> cRemove.onclick = function () { <br> self.remove(cnt); <br> }; <br><br> cAdd.id = "upload_add_" +cnt; <br> cRemove.id = "upload_remove_" +cnt; <br><br> /* 把所有生成的信息添加到容器中 */ <br> this.target.appendChild(cFile); <br> this.target.appendChild(cAdd); <br> this.target.appendChild(cRemove); <br><br> /* 计数器+1 */ <br> this._cnt++; <br><br> return this; //返回 <br>}; <br><br>upload.prototype._removeNode = function (id) { <br> var a=document.getElementById(id); <br> a.parentNode.removeChild(a); <br>}; <br><br>upload.prototype.remove = function (n) { <br> /* <br> *删除一个 file <br> */ <br> this._removeNode("upload_file_" +n); <br> /* <br> *删除一个 添加 <br> */ <br> this._removeNode("upload_add_" +n); <br> /* <br> *删除一个 删除 <br> */ <br> this._removeNode("upload_remove_" +n); <br><br> return this; <br>} <br></script>
五、OK,让我们运行一下这个控件:
<script> <br>//这里是上面我们写的控件代码,这里由于篇幅,我就不再贴了 <br></script>
<script> <br>var o=new upload("uploadConainer"); <br>o.add(); <br></script>
6. Well, you have seen the effect, but it seems not ideal. All the added things are stuck together. It is necessary to beautify it.Where to start?There are many options here:
1. Add a line break
2. Add a container div for each upload
...etc.
We add it here A container, if you want to add something in the future, it will be better to add it. Modify add:
<script> <br>upload.prototype.add = function () { <br> /* <br> *Generate a file <br> */ <br> var self = this; var cnt = this._cnt; <br> var cWrap = document.createElement("div"); <br> cWrap.id = "upload_wrap_" cnt; <br> var cFile = document.createElement("input"); <br> cFile.type="file"; cFile.name="upload"; <br> cFile.id = "upload_file_" cnt; <br> /* <br> *Generate an add<br> */ <br> var cAdd = document.createElement("span"); <br> cAdd.innerHTML="Add"; <br> cAdd.onclick = function ( ) { <br> self.add(); <br> }; <br> /* <br> *Generate a delete<br> */ <br> var cRemove = document.createElement("span"); <br> cRemove.innerHTML="Delete"; <br> cRemove.onclick = function () { <br> self.remove(cnt); <br> }; <br><br> cAdd.id = "upload_add_" cnt; <br> cRemove.id = "upload_remove_" cnt; <br><br> /* Add all generated information to the container */ <br> cWrap.appendChild(cFile); <br> cWrap.appendChild(cAdd) ; <br> cWrap.appendChild(cRemove); <br> this.target.appendChild(cWrap); <br><br> /* Counter 1 */ <br> this._cnt ; <br><br> return this ; //Return to <br>}; <br></script>
7. Add CSS to beautify it. The final code is as follows:
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
upload control - http://www.never-online.net batch upload control with javascript
tutorial of DHTML and javascript programming, Power By never-online.net