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
From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript: Exploring the Versatility of a Web LanguageJavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The Evolution of JavaScript: Current Trends and Future ProspectsThe Evolution of JavaScript: Current Trends and Future ProspectsApr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Demystifying JavaScript: What It Does and Why It MattersDemystifying JavaScript: What It Does and Why It MattersApr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software