您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

JavaScript初级教程(第五课)_基础知识

2025/5/3 19:24:43发布16次查看
现在你已经了解了计算机编程的基本知识。我们接下来继续研究一下文档对象模型(document object model-dom)。dom的点击关系起始于窗口对象在每个窗口对象中是一个文档对象(document object)。我们将重点谈谈文档对象,看看如何利用它从你的用户获得各项信息,并且动态显示新的信息。
    我们已经看过文件对象的一个属性-图象数组(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项表单。你可为图象命名,也可以以类似的方式为反馈表单命名。例如,如果该反馈表单
的编程如下:
    <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;
    更多的时候,你需要引用到表单内的元素,例如上例中的文字域。
    将鼠标滑过该链接看看会发式什么事情。
    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也可以采用类似的方法改变值:
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 swingers\noh, the jungle vip\ni've reached the top and had to stop\nand 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;
    目前我们已经学习了文字域和文字区域(值)的属性,接下来我们学习文字域和文字区域处理所用的方法:blur()、focus()和select()。
    下面的链接显示了focus() 和select()如何工作。注意他们工作一次后可能会终止功能。
mouseover to focus mouseover to select
以下为表单和链接的编码:
    <form name="method_form">
        <input type="text" name="method_text" size=40 value="hey, hey, we're the monkeys">
    </form>
    <a href="#" onmouseover="window.document.method_form.method_text.focus();">mouseover to focus</a>
    <a href="#" onmouseover="window.document.method_form.method_text.select();">mouseover to select</a>
    其使用方法和调用任何对象方法的做法是一样的:object_name.method(). 该文字域的名称是window.document.form_name.text_field_name,所以用下列语句就可调用文字域的focus()方法。 
    window.document.method_form.method_text.focus();
表单也是对象;他们有自己的方法、属性及事件处理器。其中有一项就是onsubmit。
    onsubmit的调用有以下两种情形:如果用户点击提交(submit)按钮,或用户在文字域内按了回车键,则会触发onsubmit。
    在netscape中,点击一个没有结果事件处理的submit按钮通常会导致刷新原有的页面。为了避免这种情况,你可以这样做:
    <form onsubmit="return false;">
        <input type="submit" value="submit">
    </form>
    javascript用return false阻止浏览器刷新页面。另一个例子是阻止一个href转向赋值的url 。例如:链接
    <a href="http://www.sohu.com" onclick="return false;">sohu!</a>
    不会转向任何url,因为你给onclick赋值为return false。
    以下是一个从用户获取信息的表单。在文字域中输入一些内容然后按回车键:
    who does the monkey love:
以下是表单的编码:
    <form name="text_entry_form" onsubmit="monkeylove(); return false;">
        <input type="text" name="monkey_love" size="40">
    </form>
    当你点击文字域中的回车时,onsubmit处理器被调用执行函数monkeylove(),该函数将改变文字域内的值。
如果onsubmit处理器中没有return false语句的话,执行函数monkeylove()会改变文字域内容,但由于同时网页内容会被刷新,从而又会将文字域的内容返回到原有的内容。为了阻止这种现象,就必须在onsubmit中加入return false.
    以下是monkeylove() 函数的内容:
    function monkeylove()
    {
        var who_it_is = window.document.text_entry_form.monkey_love.value;
        who_it_is = 'the monkey loves ' + who_it_is;
        window.document.text_entry_form.monkey_love.value = who_it_is;
    }
文字域和文字区域是表单的两种元素。其它的还有复选框、单选框及下拉菜单。首先我们学习复选框。
    复选框的主要属性就是:被选中(checked)。
    如果有一个名为the_form的表单,其中一个复选框的名称为the_checkbox,你可以看到如果点击该复选框会发生什么情况:
    var is_checked = window.document.the_form.the_checkbox.checked;
    if (is_checked == true)
    {
        alert(yup, it's checked!);
    } else {
        alert(nope, it's not checked.);
    }
    如果复选框被选中,则复选框的属性为真(true)。真(true)是javascript内置的数据类型,所以你不必对true添加引号。如果复选框未被选中,则其属性为假(false),这也是一只内置数据类型。
你还可以设定复选框的属性。以下是设置复选框属性的一个例子:
    click to check checkbox 1
    click to uncheck checkbox 1
    click to see the value of checkbox 1
    以下为代码:
    <form name="form_1">
        <input type="checkbox" name="check_1">checkbox 1
    </form>
    <a href="#" onclick="window.document.form_1.check_1.checked=true;return false;">click to check checkbox 1</a>
    <a href="#" onclick="window.document.form_1.check_1.checked=false;return false;">click to uncheck checkbox1</a>
    <a href="#" onclick="alert(window.document.form_1.check_1.checked);return false;">click to see the value of checkbox 1</a>
    注意我在链接中始终加入了return false,以防止浏览器刷新页面又回到原来的内容。复选框的事件处理器是onclick。当某人点击复选框时,onclick事件处理器即发挥作用。以下为其使用实例。
该例子中,表单应用了onclick复选框处理器:
    <form name="form_2">
        <input type="checkbox" name ="check_1" onclick="switchlight();">the light switch
    </form>
    当某人点击复选框时,onclick处理器被激活并执行函数switchlight(),以下为函数switchlight() 的编码(它置于网页首部中): 
    function switchlight()
    {
        var the_box = window.document.form_2.check_1;
        var the_switch = ;
        if (the_box.checked == false) {
            alert(hey! turn that back on!);     
            document.bgcolor='black';
        } else {
            alert(thanks!);
            document.bgcolor='white';
        }
    }
    第一行: 
    var the_box = window.document.form_2.check_1;
    将复选框对象赋值给一个变量。这样可以缩小编程长度,还可以将对象作为参数传递给函数。
    下一讲我们将要讲解单选框的有关知识。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product