![]() |
![]() |
JavaScript高级教程 - 第四课 | |
Thau |
|
第九页:获取难以索引的对象
你可以用下面这个函数: function swapOne() { var the_image = prompt("change parrot or cheese",""); var the_image_object; if (the_image == "parrot") { the_image_object = window.document.parrot; } else { the_image_object = window.document.cheese; } the_image_object.src = "ant.gif"; } 连同这些image标记: <img src="stuff3a/parrot.gif" name="parrot"> <img src="stuff3a/cheese.gif" name="cheese"> 请注意象这样的几行语句: the_image_object = window.document.parrot;
function swapTwo() { var the_image = prompt("change parrot or cheese",""); window.document.the_image.src = "ant.gif"; }
还好,eval能够帮你得到你想要的对象。 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"; }
JavaScript高级教程 本栏目内容归Wired Digital Inc.所有,任何单位或个人未经许可,不得擅自转载使用。 搜狐公司制作完成。 |
||||||||||||||||||||