Everything in the world is ever-changing. Object-oriented programming is also a simulation of real society. JavaScript is an object-based programming language that is very close to object-oriented programming. When dealing with JavaScript, we web designers/programmers must also face the talents of JavaScript. It can make web pages more colorful. Let’s make it clear first: JavaScript is not only used on the Web, it can be used in many fields. Of course, what I am discussing here is more about the application of JavaScript on the Web, and Mainly for event applications.
JavaScript cannot directly operate Web objects, but operates objects through the Document Object Modle (the commonly heard DOM, document model object) provided by the browser. HTML is a tree document, with the HTML tag as the root, and other elements are within the HTML tag, extending level by level. In the DOM, window is the root object, and other objects are its A child object or a child object of its child object.
First of all, let’s understand what an event is. Please look at the code below:
[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute it ]
A very simple example, The page has only one button, and its value is "This is a button", and we have assigned the onclick attribute to it. Its value is a line of JavaScript code. The alert method of the window object is used to display the content of this.value in In the warning form, what is this here? This is the object of the current operation, that is, the input object. This code tells the browser: when "the current object is clicked", window.alert(this.value) should be called. lines of code, so the browser performs related operations when the button is clicked. An object can have many events, such as click (click), double-click (dbclick), mouseover (mouseover), mousemove away (mouseout) and so on, these events can often be seen in various circulating codes. So how to set the code to be executed when the event occurs for an object? Generally speaking, there are the following three ways:
Chapter One: Directly set the event attribute of the HTML element. The name is usually the on event name. For example, the click event is onclick. For an example, please see the code above
The second one: Set the HTML object in the script. Event attributes, the name is generally the on event name, for example, obj.onclick = function, please see the example code:
[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
Or: If you need to introduce external Js, you need to refresh to execute
]
This second method is There are two ways to specify the code to be executed, but they are essentially the same. They also specify a function to the object and require the object to execute the function when a certain event occurs.
The third way: use obj. attachEvent(IE browser)/obj.addEventListener method to specify, it is recommended to use this method: [Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute ]
为什么建议第三种方式呢?上边所列的第一种方式很明显只是设置一下元素属性,只可能指定一次,而第二种方式与第三种方式的差别可以从下边的实例中看出来:
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
上边的示例使用了两个按钮,并且使用了第二种方式与第三种方式先后指定了两个函数给click事件,点击第一个按钮可以看到弹出了"指定的第二个函数",说明只有MyButton_Onclick1_2函数生效,而先前指定的MyButton_Onclick1_1被MyButton_Onclick1_2代替了。点击第二个按钮可以看到弹出了两个窗体,显示的内容分别是"指定的第一个函数"和"指定的第二个函数",很明显,使用attachEvent/addEventListener可以即增加要执行的程序并且还能够保留原有的程序,在IE中,它们是根据增加的顺序倒序执行,即先执行后添加的MyButton_Onclick2_2,然后再执行MyButton_Onclick2_1,而Firefox中则是相反,先执行MyButton_Onclick2_1然后再执行MyButton_Onclick2_2。。没办法,IE和Firefox是冤家,对着干。。呵呵
好了,俺还有项目要赶,只好先写到这里,最近比较忙,呵呵。下篇文章计划将利用本文的内容写一点应用,内容为:使用DHTML将span元素模拟成A元素,它亦是本文的后续文章,但不仅仅是关于事件了,还会配合CSS。
PS: 以上内容皆为本人所知,如有不当之处欢迎指正,不用给俺面子,文章内容严谨才不会误导读者嘛。。同时也呼吁有能力的网友也加入到原创计划中来。感谢此文的读者以及支持无忧脚本原创计划的网友。
再补充一个 IE 专有的事件驱动方法 Named Script
虽然不太建议这么写,不过做为一个介绍还是有必要的,如果在只考虑 IE 用户的情况下,此方法还是有一定的便利性的。
Named Script 通过 <script> 标签来定义对象事件。 <BR>语法: <BR><SCRIPT <BR> CLASS=classname <BR> DEFER <BR> EVENT=eventname <BR> FOR=element <BR> ID=value <BR> LANGUAGE=JAVASCRIPT | JSCRIPT | VBSCRIPT | VBS | XML <BR> SRC=url <BR> TITLE=text <BR> TYPE=MIME-type <BR>> <br><br> 其它属性与我们通常使用的 <script> 标签无异,就不过多介绍了,主要说的是 event 和 for 这两个属性。event 属性表示了脚本在什么事件触发执行,for 属性表示事件触发的对象,就以红亭的例子让我们来看试一下: <BR><div class="htmlarea"><TEXTAREA id="runcode19737"> <html> <head> <body> <input type="button" value="这是一个按钮" id="mybut" /> <script type="text/javascript" language="javascript" event="onclick" for="mybut">window.alert(this.value);</script>

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.

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

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 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 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.

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.

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.

JavaScript does not require installation because it is already built into modern browsers. You just need a text editor and a browser to get started. 1) In the browser environment, run it by embedding the HTML file through tags. 2) In the Node.js environment, after downloading and installing Node.js, run the JavaScript file through the command line.


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

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function