


Javascript & DHTML Example Programming (Tutorial) (3) Elementary Example 1—Upload File Control Example_Basic Knowledge
效果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">
batch upload control with javascript

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
