问题描述
<html>
<body>
<input type=\"text\" onFocus=\"javascript:findPosY()\"/>
<script type=\"text/javascript\">
eval( function findPosY(){alert(\"OK\");});
</script>
</body>
</html>
在执行上述代码时,未调用eval()函数,也不会显示警报。
解决方法
您的代码进行了两次评估。首先将“ 1”用于内联事件处理程序(通过这种方式,不需要“ 2”部分)。接下来,您尝试在脚本标签内将ѭ1。如其他答案所示,“ 1”需要一个字符串。但是在这种情况下,完全没有必要使用“ 1”。您的代码段应该这样写:
<html>
<body>
<input type=\"text\" onfocus=\"findPosY()\"/>
<script type=\"text/javascript\">
function findPosY(){
alert(\"OK\");
}
</script>
</body>
</html>
, eval()
评估一串JavaScript代码...
资源。
, 您应该将一个字符串传递给eval函数:
<html>
<body>
<input type=\"text\" onFocus=\"javascript:findPosY()\"/>
<script type=\"text/javascript\">
eval( \'function findPosY(){alert(\"OK\");}\');
</script>
</body>
</html>
, <html>
<body>
<input type=\"text\" onFocus=\"javascript:findPosY()\"/>
<script type=\"text/javascript\">
eval(\'(function (){alert(\"OK\");})()\');
</script>
</body>
</html>
请注意,在要评估的字符串末尾加上括号,这将调用该函数。