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:
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:
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 ] 为什么建议第三种方式呢?上边所列的第一种方式很明显只是设置一下元素属性,只可能指定一次,而第二种方式与第三种方式的差别可以从下边的实例中看出来:
这个例子有多个相同的 name 的对象,(注意:是 name 而不是 id,在 DOM 标准中 id 应该是唯一的,虽然在 IE 中可以有多个相同 id 的对象,不过不建议大家这么做。)只用一句 Named Script 就可以对这些对象完成事件驱动,从代码上来说简便了不少,及至以后的修改也变的非常方便,这也是 Named Script 的最大优点。
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