search
HomeWeb Front-endJS TutorialJavascript & 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"类。

也许对于单单看前几章的朋友来说这个例子过于深奥了,但是不用担心,一步步来解释应该很快理解的,关键是理解怎么做,而不是怎么写。

如果还有不懂的朋友,可以留言给我。
首先看一个成品截图预览:

http://www.never-online.net/tutorial/js/upload/upload_preview.png


一、接下来我们先说思路,首先定义一个upload"类",

一)、这个类的公共访问信息应该有:
1、构造函数中要传递一些必要的参数,比如,在哪个容器构造upload的信息。
2、必须有一个add()方法,用于添加一个upload
3、必须有一个remove()方法,用于删除一个upload

二)、这个类中应该有一些必要的信息,是生成实例本身所具有的信息,(upload对象的一些信息)。
1、得到一共多少个upload信息,
2、一个容器对象,这个对象也是从构造函数中传递。

整个图可以简单的表示为

http://www.never-online.net/tutorial/js/upload/upload_UML.png


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>&lt ;/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:

BR>"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

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
JavaScript Comments: A Guide to Using // and /* */JavaScript Comments: A Guide to Using // and /* */May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool