JavaScript初级教程第五课
发布时间:2023-02-16 12:44:51 所属栏目:Java 来源:互联网
导读:我们已经看过文件对象的一个属性-图象数组(Images array)。在第3课中,文件中第1个图象可以通过改变其src属性被修改。 例: window.document.images[0].src=some_new_picture.gif; 该命令将把文档中的第1个图象改成一个名为some_new_picture.gif的新图象
我们已经看过文件对象的一个属性-图象数组(Images array)。在第3课中,文件中第1个图象可以通过改变其src属性被修改。 例: window.document.images[0].src='some_new_picture.gif'; 该命令将把文档中的第1个图象改成一个名为some_new_picture.gif的新图象。在DOM中,图象数组中的每个图象也是对象。所以images[0].src指令象对对象那样发挥作用,它的意思就是:从Image数组中调用Image的中的对象image[0],并设定其src属性。将其翻译过来即:从该窗口中调用文档(document)属性,从该文档的图象数组中调用第1幅图象,并将其src属性该为图象some_new_picture.gif。 Image对象还有其它很多有趣的属性,例如,你可以让JavaScript在作其它事之前检查一幅图象是否已经完全载入。但是,我们只能在以后的课程中谈到这些属性。今天,我们将谈谈反馈表单及如何在JavaScript中应用反馈表单。 反馈表单是HTML 1.0规范的一部分。许多人对其并不很了解。很多人只以为它只能由于用户端CGI编程。其实,即使你不是CGI程序员,反馈表单也会为你提供许多功能。而JavaScript可被用来为反馈表单添加各种功能。而且无须用户端CGI的辅助。 如果你不了解反馈表单如何工作,请学习HTML入门中的有关课程。然后在开始学习本课。 首先,在javascript中,反馈表单也被存储在一个对象数组中。你可以通过window.document.images[0]调用图象数组中的第一幅图象,你也可以用window.document.forms[0]调用反馈表单数组中的第1项表单。你可为图象命名,也可以以类似的方式为反馈表单命名。例如,如果该反馈表单 Power to the primates! 的编程如下: <form name="first_form"> <input type="text" name="first_text" size="40" value="Power to the primates!"> </form> 你可以用以下两种方式之一引用该表单: var the_form = window.document.forms[0]; var the_same_form = window.document.first_form; 更多的时候,你需要引用到表单内的元素,例如上例中的文字域。 将鼠标滑过该链接看看会发式什么事情。 Sour puss! Yes, and I know it. No! 通过改变文字域的值就可实现这种奇妙的变换。 <form name="second_form"> <input type="text" name="first_text" value="Are you happy?"> </form> 改变文字域的链接为: <a href="#" onMouseOver="window.document.second_form.first_text.value='Clap clap!';">Yes, and I know it.</a> <a href="#" onMouseOver="window.document.second_form.first_text.value='Sour puss!';">No!</a> 意思是说,表单调用第1个表单,并将其值设为 'Clap clap!'第2行作用相似。这同改变图象的src非常相似。只不过文字域改变的是值。 对textareas也可以采用类似的方法改变值: I wanna be a Webmonkey, mancub And stroll right into town And be just like the other Webmonkeys I wanna start monkeyin' around! Part 1 Part 2 表单编码: <form name="third_form"> <textarea name="the_textarea" rows=10 cols=60> Mouse over below to see the first verse of The Webmonkey song, adapted from "I Wanna Be Like You" (The Monkey Song) from Walt Disney's The Jungle Book written by Richard M. Sherman and Robert B. Sherman </textarea> </form> 注意该表单有一个名字:third_form,并且文字区也有一个名字:the_textarea。 其链接和文字域的设定方法基本相同: <a href="#" onMouseOver="window.document.third_form.the_textarea.value=first_part;">Part 1</a> <a href="#" onMouseOver="window.document.third_form.the_textarea.value=second_part;">Part 2</a> 唯一的不同之处在于将一个变量赋值给textareas,而不是将字符串赋值给它。该变量在HTML首部已经做了定义。 以下是变量的字符串赋值: var first_part = "Now I'm the king of the swingersnOh, the jungle VIPnI've reached the top and had to stopnAnd that's what botherin' me"; 注意"n"是换行符。如果你在一个<pre> 并且或在一个textarea中写字,"n" 换行符非常方便。 除了改变表单元素的值,JavaScript还可以让你检测在表单中进行的事件。下一讲将进行介绍。 文字域可以链接onBlur、onFocus和onChange事件。当有人点击文字域的里边时则发生onFocus事件。而如果点击文字域的外面或按了tab键时则发生onblur事件。如果有人改变了文字域内的内容然后转到文字域外部的区域时则发生onChange事件。 试着做这些事情看下面的文字域会发生什么情况。 以下是编制方法:文字域的编码: <input type="text" name="first_text" onFocus="writeIt('focus');" onBlur="writeIt('blur');" onChange="writeIt('change');"> 每一个事件处理器调用函数writeIt(),该函数已作了定义。编码如下: <script language="JavaScript"> <!-- hide me function writeIt(the_word) { var word_with_return = the_word + "n"; window.document.first_form.the_textarea.value += word_with_return; } // show me --> </script> 前几行是典型的JavaScript预定义。主体中的第1行 var word_with_return = the_word + "n"; 将一个变量word_with_return进行初始化为函数处理后的字符串并加上换行符"n".。注意"n" 是标准的计算机指令。 下一行 window.document.first_form.the_textarea.value += word_with_return; 将文字区域的值设置为其原值加新变量。其作用相当于 window.document.first_form.the_textarea.value = window.document.first_form.the_textarea.value + word_with_return; (编辑:十堰站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |