
为什么javascript使用EVAL函数就提示未定义?
<formname="the_form"><selectname="choose_category"><option>sdfds<option>sdfdsdd</sele...
<form name="the_form">
<select name="choose_category">
<option>sdfds
<option>sdfdsdd
</select>
</form>
<script>
document.write(eval(window.document.the_form.choose_category.options[0].text));
</script>
执行这代码看看 不要删除eval函数
为什么会提示错误 未定义呢? 展开
<select name="choose_category">
<option>sdfds
<option>sdfdsdd
</select>
</form>
<script>
document.write(eval(window.document.the_form.choose_category.options[0].text));
</script>
执行这代码看看 不要删除eval函数
为什么会提示错误 未定义呢? 展开
3个回答
展开全部
Eval 函数
功能:先解释Javascript代码,然后在执行它
用法:Eval(codeString)
codeString是包含有Javascript语句的字符串,在eval之后使用Javascript引擎编译。
举个小例子:
var the_unevaled_answer = "2 + 3";
var the_evaled_answer = eval("2 + 3");
alert("the un-evaled answer is " + the_unevaled_answer + " and the evaled answer is " + the_evaled_answer);
如果你运行这段eval程序, 你将会看到在JavaScript里字符串"2 + 3"实际上被执行了。所以当你把the_evaled_answer的值设成 eval("2 + 3")时, JavaScript将会明白并把2和3的和返回给the_evaled_answer。
这个看起来似乎有点傻,其实可以做出很有趣的事。比如使用eval你可以根据用户的输入直接创建函数。这可以使程序根据时间或用户输入的不同而使程序本身发生变化,通过举一反
三,你可以获得惊人的效果。
在实际中,eval很少被用到,但也许你见过有人使用eval来获取难以索引的对象。 文档对象模型(DOM)的问题之一是:有时你要获取你要求的对象简直就是痛苦。例如,这里有一个函数询问用户要变换哪个图象:变换哪个图象你可以用下面这个函数:
?1234567891011121314 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">
功能:先解释Javascript代码,然后在执行它
用法:Eval(codeString)
codeString是包含有Javascript语句的字符串,在eval之后使用Javascript引擎编译。
举个小例子:
var the_unevaled_answer = "2 + 3";
var the_evaled_answer = eval("2 + 3");
alert("the un-evaled answer is " + the_unevaled_answer + " and the evaled answer is " + the_evaled_answer);
如果你运行这段eval程序, 你将会看到在JavaScript里字符串"2 + 3"实际上被执行了。所以当你把the_evaled_answer的值设成 eval("2 + 3")时, JavaScript将会明白并把2和3的和返回给the_evaled_answer。
这个看起来似乎有点傻,其实可以做出很有趣的事。比如使用eval你可以根据用户的输入直接创建函数。这可以使程序根据时间或用户输入的不同而使程序本身发生变化,通过举一反
三,你可以获得惊人的效果。
在实际中,eval很少被用到,但也许你见过有人使用eval来获取难以索引的对象。 文档对象模型(DOM)的问题之一是:有时你要获取你要求的对象简直就是痛苦。例如,这里有一个函数询问用户要变换哪个图象:变换哪个图象你可以用下面这个函数:
?1234567891011121314 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">
展开全部
换成
eval(window.document.the_form.choose_category.options[0]).text
把.text写在外面,否则eval会把options[0]的值视做变量
eval(window.document.the_form.choose_category.options[0]).text
把.text写在外面,否则eval会把options[0]的值视做变量
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
应该不需要eval.
eval是把一段字符串当做js代码执行,你window.document.the_form。。。本身你没当字符串定义
eval是把一段字符串当做js代码执行,你window.document.the_form。。。本身你没当字符串定义
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询