Home > Web Front-end > JS Tutorial > JScript event-driven programming_javascript skills

JScript event-driven programming_javascript skills

WBOY
Release: 2016-05-16 19:20:52
Original
1082 people have browsed it

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:

[Ctrl A Select all Note:
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"> <input type="button" value="这是一个按钮" id="mybut"> <script type="text/javascript" language="javascript" event="onclick" for="mybut">window.alert(this.value);</script>
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

大家应该看到了,效果和其它的方法是一样的,也许有人会说既然一样而且兼容性又不好,为什么还要这么用呢?请看下面的例子:

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

这个例子有多个相同的 name 的对象,(注意:是 name 而不是 id,在 DOM 标准中 id 应该是唯一的,虽然在 IE 中可以有多个相同 id 的对象,不过不建议大家这么做。)只用一句 Named Script 就可以对这些对象完成事件驱动,从代码上来说简便了不少,及至以后的修改也变的非常方便,这也是 Named Script 的最大优点。

  应用最多的地方会在表单的 Radio 或者 Checkbox 对象上,因为它们一般都会有相同的 name,而同时也会有相同的事件驱动,具体的应用范围很广,可以在无忧脚本 http://www.51j.com/ 里搜索到大量的应用实例,在此仅做一些简单的介绍,就不过多的举例了。  最后再次重申一下,此方法仅为 IE 特有,其它其它浏览器不支持。
判断window.document.all是否为null是判断当前浏览器是IE还是Firefox最简单的方法,因为IE有这个对象,而firefox没有。
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template