我之前曾經在項目中寫過JS+ASP.NET多相片上傳時添加上傳框的處理。
但是,現在回頭一看,我那時雖然功能上完全實現了添加過程,但是程式完全是過程化,完全是就事論事。
如果現在需要複用或壯大的話,不得不從頭再來。
原文地址:http://www.cnblogs.com/McJeremy/archive/2008/05/22/1205007.html
那麼,如何把添加的過程封裝成一個類以方便複用或壯大呢?
本來打算自己實現一下,但是在網上看到這樣一篇文章,
來源地址:http://www.never-online.net/blog/article.asp?id=154
我把其內容複寫在這裡備忘(申明:此文非本人原創,版塊歸源作者所有
--------------------華麗的分割線----------------------------
首先看一個成品預覽:
一、接下來我們先說思路,首先定義一個upload"類",
一)、這個類的公用訪問資訊應該有:
1、建構函式中要傳遞一些必要的參數,比如,在哪個容器構造upload的資訊。
2、必須有一個add()方法,用於添加一個upload
3、必須有一個remove()方法,用於刪除一個upload
二)、這個類中應該有一些必要的資訊,是產生執行個體本身所具有的資訊,(upload對象的一些資訊)。
1、得到一共多少個upload資訊,
2、一個容器物件,這個對象也是從建構函式中傳遞。
整個圖可以簡單的表示為
二、我想我們該想想應該用到哪些知識,哪些是熟悉的,哪些是未知的。
一)、正如我們上面預覽圖所見到的,需要三個或以上的新控制項。(添加,刪除,還有一個file控制項,也或者還有其它的...但至少眼睛見到的就這麼多了),既然是新的資訊,就會可能用到document.createElement,要添加進一個容器裡就可能用到object.appendChild(obj)或者obj.insertBefore()方法。刪除也就是obj.parentNode.removeChild(obj)。這些上一章都已經說過了。
二)、既然是控制項,肯定得用function或者是一個對象(object)封裝起來,對這部分知識,第一章已經簡單的說明了
三)、如何組織呢?在上面的思路中也已經有了文字和圖示
接下來就動手寫:
一)、建構函式,以及基本的代碼(虛擬碼)
Copy Code(拷貝代碼)-Run HTML(運行代碼)-Save Code(另存代碼)<script>
function upload(target/*容器*/
)
{
this._cnt = 0; /*計數器*/
this.target = document.getElementById(target);
};
upload.prototype.add = function () {
/*
*產生一個 file
*產生一個 添加
*產生一個 刪除
*計數器+1
*/
};
upload.prototype.remove = function () {
/*
*刪除一個 file
*刪除一個 添加
*刪除一個 刪除
*/
};
</script>
二、寫出add方法的實現
Copy Code(拷貝代碼)-Run HTML(運行代碼)-Save Code(另存代碼)<script>
upload.prototype.add = function () {
/*
*產生一個 file
*/
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++;
return this; //返回
};
</script>
三、寫出remove方法的實現
Copy Code(拷貝代碼)-Run HTML(運行代碼)-Save Code(另存代碼)<script>
upload.prototype.remove = function (n) {
/*
*刪除一個 file
*/
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);
return this;
}
</script>
上面remove方法過於重複,可考慮重新把remove再簡化,從而使我們的代碼更簡短而且易於維護呢?在這裡,我們把這個通用功能放到一個函數裡,也就是多加一個函數:
Copy Code(拷貝代碼)-Run HTML(運行代碼)-Save Code(另存代碼)<script>
upload.prototype._removeNode = function (id) {
var a=document.getElementById(id);
a.parentNode.removeChild(a);
};
upload.prototype.remove = function (n) {
/*
*刪除一個 file
*/
this._removeNode("upload_file_" +n);
/*
*刪除一個 添加
*/
this._removeNode("upload_add_" +n);
/*
*刪除一個 刪除
*/
this._removeNode("upload_remove_" +n);
return this;
}
</script>
四、將程式碼群組合一下,基本上可以算是完成了:D
Copy Code(拷貝代碼)-Run HTML(運行代碼)-Save Code(另存代碼)<script>
function upload(target/*容器*/
)
{
this._cnt = 0; /*計數器*/
this.target = document.getElementById(target);
};
upload.prototype.add = function () {
/*
*產生一個 file
*/
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++;
return this; //返回
};
upload.prototype._removeNode = function (id) {
var a=document.getElementById(id);
a.parentNode.removeChild(a);
};
upload.prototype.remove = function (n) {
/*
*刪除一個 file
*/
this._removeNode("upload_file_" +n);
/*
*刪除一個 添加
*/
this._removeNode("upload_add_" +n);
/*
*刪除一個 刪除
*/
this._removeNode("upload_remove_" +n);
return this;
}
</script>
五、OK,讓我們運行一下這個控制項:
Copy Code(拷貝代碼)-Run HTML(運行代碼)-Save Code(另存代碼)<html>
<head>
<script>
//這裡是上面我們寫的控制項代碼,這裡由於篇幅,我就不再貼了
</script>
</head>
<body>
<div id="uploadContainer"></div>
<script>
var o=new upload("uploadConainer");
o.add();
</script>
</body>
</html>
六、嗯,已經看到效果了吧,但似乎不太理想,全部添加的都粘在一起了,有必要要美化一下。從何處入手?這裡可以有很多選擇:
1、加一個分行符號<br>
2、每添加一個upload就再加一個容器div
...等
我們這裡添加一個容器,如果以後還要加什麼東西,會更好加一些,修改add:
Copy Code(拷貝代碼)-Run HTML(運行代碼)-Save Code(另存代碼)<script>
upload.prototype.add = function () {
/*
*產生一個 file
*/
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++;
return this; //返回
};
</script>
七、加上CSS美化一下,最後的代碼如下:
Copy Code(拷貝代碼)-Run HTML(運行代碼)-Save Code(另存代碼)<!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++;
return this;
};
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);
return this;
};
onload = function () {
var o = new upload("container");
o.add();
};
//]]>
</script>
</head>
<body id="www.never-online.net">
<h1> batch upload control with javascript </h1>
<div id="container"></div>
<div id="footer">tutorial of DHTML and javascript programming, Power By never-online.net</div>
</body>
</html>
--------------------華麗的分割線----------------------------
對比上面的文章,覺得我當初的實現真的很傻*啊。)