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

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

2025/6/8 3:45:59发布9次查看
在javascript中单选框的用法和复选框相似。不同之处在于html中的应用。复选框是一种开关。如果一个复选框被选中,你可以再点击后取消选取. 但如果单选框被选中,则只能通过选取另外一个单选框才能取消对该单选框的选取。例:
    在该例中,如果你打算取消对一个单选框的选取,你必须点击另一个单选框。再看下面的程序:
<form name="form_1">
        <input type="radio" name ="radio_1" onclick="offbutton();">light off
        <input type="radio" name ="radio_2" onclick="onbutton();" checked>light on
    </form>
    当第一个单选框被选中时,函数offbutton() 被调用。函数如下: 
    function offbutton()
    {
        var the_box = window.document.form_1.radio_1;
        if (the_box.checked == true)
        {
            window.document.form_1.radio_2.checked = false;
            document.bgcolor='black';
            alert(hey! turn that back on!);
        }
    }
    这个例子很象前面的复选框例子,主要的区别在于该行: 
    window.document.form_1.radio_2.checked = false;
    该行指令指示javascript在该按钮被点击时关闭另外一个按钮。由于另外一个按钮的函数同这个很相似:
    function onbutton()
    {
        var the_box = window.document.form_1.radio_2;
        if (the_box.checked == true)
        {
            window.document.form_1.radio_1.checked = false;
            document.bgcolor='white';
            alert(thanks!);
        }
    }
选单是我们所学的表单选项中最奇特的一种。有两种基本的格式:下列选单和列表选单。以下为例子:
    奇特之处在于这个选单有名称,但其中的各个选项没有名称。例如,在html中,第1个选单如下:
  <select name="pulldown_1" size="1">
    <option>probiscus </option>
    <option>spider </option>
    <option>lemur </option>
    <option>chimp </option>
    <option>gorilla </option>
    <option>orangutan </option>
  </select>
    注意这个选单的名称是pulldown_1,但各个选项没有名称。所以要调用其中的各个选项则有点困难。
    幸好数组可以帮助我们调用其中的选项。如果你想改变该下列选单中的第2个选项,你可以这样做:
    window.document.form_1.pulldown_1.options[1].text = 'new_text';
    这是因为选单的元素有一个选项属性,而该属性是选单所有选项的集合而成的数组。点击change the selectt然后从下拉选单从下列选单中查看其选项是否已经被改变。现在第2个选项应该是*thau*。
    除了选项属性,选单还有一项属性叫做selectedindex。一个选项被选择后,selectedindex属性将变成被选项的数组索引号(序列号)。选择第2个列表选单中的一个选项,然后检查索引号。记住数组中的第1个选项的索引号是0。
    <a href="#" onclick="alert('index is: ' + window.document.form_1.list_1.selectedindex);return false;">check the index.</a>
    表单的名称是form_1,列表选单的名称是list_1。selectedindex属性值为window.document.form_1.list_1.selectedindex。你还可
以将selectedindex设置如下:
    window.document.form_1.list_1.selectedindex = 1;
    并高亮度显示第2个选项。
    一旦你得到被选项的索引号,你就可以发现其内容:
    var the_select = window.document.form_1.list_1;
    var the_index = the_select.selectedindex;
    var the_selected = the_select.options[the_index].text;
    selectedindex属性很有用,但如果有多个选项同时被选中,会如何呢?
选单元素的处理器为onchange()。当选单发生变化时,则该处理器被激活。
尝试这个例子并阅读下面的注释:
my favorite animal is ...
    注释一个比较复杂的javascript程序。首先,我们看看表单本身:
    <form name="the_form">
        <select name="choose_category" onchange="swapoptions(window.document.the_form.choose_category.options[selectedindex].text);">
            <option selected>dogs</option>
            <option>fish</option>
            <option>birds</option>
        </select>
        <select name="the_examples" multiple>
            <option>poodle</option>
            <option>puli</option>
            <option>greyhound</option>
        </select>
    </form>
    该表单有两个元素:一个下拉选单和一个列表选单。下列选单的处理器调用函数swapoptions()。该函数在首部已经
作了定义,其参数为- 被选的动物种类。
    首部中我首先定义的几个数组:
    var dogs = new array(poodle,puli,greyhound);
    var fish = new array(trout, mackerel, bass);
    var birds = new array(robin, hummingbird, crow);
    注意这些数组的命名和下拉选单中的命名一致。很快你就会明白为什么。现在我们看看当下拉选单被改变时被调用的函数:
    function swapoptions(the_array_name)
    {
        var numbers_select = window.document.the_form.the_examples;
        var the_array = eval(the_array_name);
        setoptiontext(window.document.the_form.the_examples, the_array);
    }
    该函数的定义包括一个参数:the_array_name。如果打开下拉选单并选择fish ,则the_array_name就等同于字符串fish。
    函数主体中第1行包括一个变量用于引用第2个表单元素:列表选单。
    第2行引入一个新概念:eval()。eval()比较奇特,我们留在以后的课程中讲解。第2行命令的这些结果是变量the_array将等同于前面所定义的数组之一。如果the_array_name是fish,the_array则等同于fish数组。如果你想了解这是怎么作的,请学习eval。
    第3行定义另一个函数setoptiontext()。setoptiontext()用于将the_array赋值给列表选单。以下为该函数内容:
    function setoptiontext(the_select, the_array)
    {
        for (loop=0; loop < the_select.options.length; loop++)
        {
            the_select.options[loop].text = the_array[loop];
        }
    }
    该函数有两个参数:对选单元素的引用和一个数组。第1行设立一个for循环,由于循环所有的选单元素。注意选单元素的选项属性是该选单所有选项组成的数组。因为它是一个数组,你可以用长度(length)属性发现数组的元素数目。第1次循环时,循环变量值是0。循环的主体值为:
the_select.options[0].text = the_array[0];
    如果你在下拉选单中选择了fish,则the_array[0]就是trout,所以该行命令将列表选单中的第1个选项改成trout 。下一次循环时,循环等于1,并且列表选单中第2个选项则是 mackerel 。
    如果你理解了该例子,你对javascript的了解就比较深了。 
    初级教程到此结束,随后要发布进阶教程,敬请关注。
该用户其它信息

VIP推荐

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