Javascript & DHTML 實(shí)例編程(教程)(三)初級實(shí)例篇1—上傳文件控件實(shí)例
更新時間:2007年06月02日 00:00:00 作者:
效果DEMO:
http://www.never-online.net/tutorial/js/upload/
Javascript & DHTML 實(shí)例編程(教程)(三),初級實(shí)例篇—上傳文件控件實(shí)例
上章基本上把要交代的基本知識都說了一些,今天終于開始寫代碼了:D
首先來做一個實(shí)例,批量上傳的UI控件。以后一般做的示例也是以UI控件為主的。都是封裝成Object或者用Function封裝成"Class"類。
也許對于單單看前幾章的朋友來說這個例子過于深奧了,但是不用擔(dān)心,一步步來解釋應(yīng)該很快理解的,關(guān)鍵是理解怎么做,而不是怎么寫。
如果還有不懂的朋友,可以留言給我。
首先看一個成品截圖預(yù)覽:
一、接下來我們先說思路,首先定義一個upload"類",
一)、這個類的公共訪問信息應(yīng)該有:
1、構(gòu)造函數(shù)中要傳遞一些必要的參數(shù),比如,在哪個容器構(gòu)造upload的信息。
2、必須有一個add()方法,用于添加一個upload
3、必須有一個remove()方法,用于刪除一個upload
二)、這個類中應(yīng)該有一些必要的信息,是生成實(shí)例本身所具有的信息,(upload對象的一些信息)。
1、得到一共多少個upload信息,
2、一個容器對象,這個對象也是從構(gòu)造函數(shù)中傳遞。
整個圖可以簡單的表示為
二、我想我們該想想應(yīng)該用到哪些知識,哪些是熟悉的,哪些是未知的。
一)、正如我們上面預(yù)覽圖所見到的,需要三個或以上的新控件。(添加,刪除,還有一個file控件,也或者還有其它的...但至少眼睛見到的就這么多了),既然是新的信息,就會可能用到document.createElement,要添加進(jìn)一個容器里就可能用到object.appendChild(obj)或者obj.insertBefore()方法。刪除也就是obj.parentNode.removeChild(obj)。這些上一章都已經(jīng)說過了。
二)、既然是控件,肯定得用function或者是一個對象(object)封裝起來,對這部分知識,第一章已經(jīng)簡單的說明了
三)、如何組織呢?在上面的思路中也已經(jīng)有了文字和圖示
接下來就動手寫:
一)、構(gòu)造函數(shù),以及基本的代碼(偽代碼)
<script>
function upload(target/*容器*/
)
{
this._cnt = 0; /*計數(shù)器*/
this.target = document.getElementById(target);
};
upload.prototype.add = function () {
/*
*生成一個 file
*生成一個 添加
*生成一個 刪除
*計數(shù)器+1
*/
};
upload.prototype.remove = function () {
/*
*刪除一個 file
*刪除一個 添加
*刪除一個 刪除
*/
};
</script>
二、寫出add方法的實(shí)現(xiàn)
<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);
/* 計數(shù)器+1 */
this._cnt++;
return this; //返回
};
</script>
三、寫出remove方法的實(shí)現(xiàn)
<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方法過于重復(fù),可考慮重新把remove再簡化,從而使我們的代碼更簡短而且易于維護(hù)呢?在這里,我們把這個通用功能放到一個函數(shù)里,也就是多加一個函數(shù):
<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
<script>
function upload(target/*容器*/
)
{
this._cnt = 0; /*計數(shù)器*/
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);
/* 計數(shù)器+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,讓我們運(yùn)行一下這個控件:
<html>
<head>
<script>
//這里是上面我們寫的控件代碼,這里由于篇幅,我就不再貼了
</script>
</head>
<body>
<div id="uploadContainer"></div>
<script>
var o=new upload("uploadConainer");
o.add();
</script>
</body>
</html>
六、嗯,已經(jīng)看到效果了吧,但似乎不太理想,全部添加的都粘在一起了,有必要要美化一下。從何處入手?這里可以有很多選擇:
1、加一個換行符<br>
2、每添加一個upload就再加一個容器div
...等
我們這里添加一個容器,如果以后還要加什么東西,會更好加一些,修改add:
<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);
/* 計數(shù)器+1 */
this._cnt++;
return this; //返回
};
</script>
七、加上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++;
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>
http://www.never-online.net/tutorial/js/upload/
Javascript & DHTML 實(shí)例編程(教程)(三),初級實(shí)例篇—上傳文件控件實(shí)例
上章基本上把要交代的基本知識都說了一些,今天終于開始寫代碼了:D
首先來做一個實(shí)例,批量上傳的UI控件。以后一般做的示例也是以UI控件為主的。都是封裝成Object或者用Function封裝成"Class"類。
也許對于單單看前幾章的朋友來說這個例子過于深奧了,但是不用擔(dān)心,一步步來解釋應(yīng)該很快理解的,關(guān)鍵是理解怎么做,而不是怎么寫。
如果還有不懂的朋友,可以留言給我。
首先看一個成品截圖預(yù)覽:
一、接下來我們先說思路,首先定義一個upload"類",
一)、這個類的公共訪問信息應(yīng)該有:
1、構(gòu)造函數(shù)中要傳遞一些必要的參數(shù),比如,在哪個容器構(gòu)造upload的信息。
2、必須有一個add()方法,用于添加一個upload
3、必須有一個remove()方法,用于刪除一個upload
二)、這個類中應(yīng)該有一些必要的信息,是生成實(shí)例本身所具有的信息,(upload對象的一些信息)。
1、得到一共多少個upload信息,
2、一個容器對象,這個對象也是從構(gòu)造函數(shù)中傳遞。
整個圖可以簡單的表示為
二、我想我們該想想應(yīng)該用到哪些知識,哪些是熟悉的,哪些是未知的。
一)、正如我們上面預(yù)覽圖所見到的,需要三個或以上的新控件。(添加,刪除,還有一個file控件,也或者還有其它的...但至少眼睛見到的就這么多了),既然是新的信息,就會可能用到document.createElement,要添加進(jìn)一個容器里就可能用到object.appendChild(obj)或者obj.insertBefore()方法。刪除也就是obj.parentNode.removeChild(obj)。這些上一章都已經(jīng)說過了。
二)、既然是控件,肯定得用function或者是一個對象(object)封裝起來,對這部分知識,第一章已經(jīng)簡單的說明了
三)、如何組織呢?在上面的思路中也已經(jīng)有了文字和圖示
接下來就動手寫:
一)、構(gòu)造函數(shù),以及基本的代碼(偽代碼)
<script>
function upload(target/*容器*/
)
{
this._cnt = 0; /*計數(shù)器*/
this.target = document.getElementById(target);
};
upload.prototype.add = function () {
/*
*生成一個 file
*生成一個 添加
*生成一個 刪除
*計數(shù)器+1
*/
};
upload.prototype.remove = function () {
/*
*刪除一個 file
*刪除一個 添加
*刪除一個 刪除
*/
};
</script>
二、寫出add方法的實(shí)現(xiàn)
<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);
/* 計數(shù)器+1 */
this._cnt++;
return this; //返回
};
</script>
三、寫出remove方法的實(shí)現(xiàn)
<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方法過于重復(fù),可考慮重新把remove再簡化,從而使我們的代碼更簡短而且易于維護(hù)呢?在這里,我們把這個通用功能放到一個函數(shù)里,也就是多加一個函數(shù):
<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
<script>
function upload(target/*容器*/
)
{
this._cnt = 0; /*計數(shù)器*/
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);
/* 計數(shù)器+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,讓我們運(yùn)行一下這個控件:
<html>
<head>
<script>
//這里是上面我們寫的控件代碼,這里由于篇幅,我就不再貼了
</script>
</head>
<body>
<div id="uploadContainer"></div>
<script>
var o=new upload("uploadConainer");
o.add();
</script>
</body>
</html>
六、嗯,已經(jīng)看到效果了吧,但似乎不太理想,全部添加的都粘在一起了,有必要要美化一下。從何處入手?這里可以有很多選擇:
1、加一個換行符<br>
2、每添加一個upload就再加一個容器div
...等
我們這里添加一個容器,如果以后還要加什么東西,會更好加一些,修改add:
<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);
/* 計數(shù)器+1 */
this._cnt++;
return this; //返回
};
</script>
七、加上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++;
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>
相關(guān)文章
js split 的用法和定義 js split分割字符串成數(shù)組的實(shí)例代碼
關(guān)于js split的用法,我們經(jīng)常用來將字符串分割為數(shù)組方便后續(xù)操作,今天寫一段廣告判斷代碼的時候,竟然忘了split的用法了,特整理下,方便需要的朋友2012-05-05Javascript學(xué)習(xí)筆記之 對象篇(一) : 對象的使用和屬性
Javascript 中的一切都可以視為對象,除了兩個特例:null 和 undefined。2014-06-06JavaScript Math.ceil 方法(對數(shù)值向上取整)
js Math.ceil用于對數(shù)值向上取整,即得到大于或等于該數(shù)值的最小整數(shù),需要的朋友可以參考下2015-01-01一文帶你了解JavaScript基礎(chǔ)之深拷貝和淺拷貝
這篇文章主要為大家介紹了JavaScript深拷貝和淺拷貝,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-12-12詳解JavaScript中setSeconds()方法的使用
這篇文章主要介紹了詳解JavaScript中setSeconds()方法的使用,是JS入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-06-06