JavaScript高级教程 - 第四课
Thau
教程JavaScript
网站设计
HTML
JavaScript
样式表
动态HTML
XML
图形与字体
多媒体
Java
后端编程
浏览器
电子商务
其它资源
Web 入门
软件下载

第十页:另一种获取难以索引的对象的手段

 

这就是我们迄今为止学到的获取图象对象的手段:
function simpleSwap()
{
	var the_image = prompt("change parrot or cheese","");
	var the_image_name = "window.document." + the_image;
	var the_image_object = eval(the_image_name);
	the_image_object.src = "ant.gif";
}
从这里延伸一下,你也可以通过图象的名字在图象关联数组
中引用图象,比如:window.document.images
["parrot"].src
. 这就象通过数组的下标引用图象一样,如
window.document.images[0].src. 所以,上面的代码可
以重新写成:
function simpleSwap()
{
	var the_image = prompt("change parrot or cheese","");
	window.document.images[the_image].src = "ant.gif";
}
你可以用这种技巧获得你的各种对象。如果在一个表单里有
一个文本框,象这样:
          
<form name="the_form">
<input type="text" name="the_text_box">
</form>

你可以用这个来改变文本框里的文本:

window.document.forms["the_form"].elements["the_text_box"].value = "hello!";

现在你已经知道了好几种获取和改变对象信息的途径了。在
上面的例子里,我们可以用四种手段来设置文本框的文本:


var the_form_name = "the_form";
var the_element_name = "the_text_box";
  1. window.document.forms[0].elements[0].value = "hello!";
  2. window.document.forms[the_form_name].elements[the_element_name].value = "hello!";
  3. window.document.the_form.the_text_box.value = "hello!";
  4. var the_element_string = "window.document." + the_form_name + "." + the_element_name; var the_element = eval(the_element_string); the_element_string.value = "hello!";
具体到你用哪个方法也许取决于你的心情以及你能想起来用
那一个。

这就是我们今天的课程,我们来复习一下。>>

JavaScript高级教程
第一页 JavaScript高级教程- 4
第二页 图象映射与JavaScript
第三页 预装图象 - 是什么?
第四页 预装图象 - 怎么做?
第五页 对象的优点
第六页 创建你自己的对象
第七页 你的面向对象的虚拟宠物
第八页 计算字符串
第九页 获取难以索引的对象
第十页 另一种获取难以索引的对象的手段
第十一页 第四天课程复习

[第1课][第2课][第3课][第4课][第5课]


本栏目内容归Wired Digital Inc.所有,任何单位或个人未经许可,不得擅自转载使用。 搜狐公司制作完成。