码迷,mamicode.com
首页 > 其他好文 > 详细

eval函数应用

时间:2015-12-16 18:53:01      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
    
    <img src="../image/desktop/1.jpg" width="200" height="100" name="aaa"/>
    <img src="../image/desktop/2.jpg" width="200" height="100" name="bbb"/>
    <img src="../image/desktop/3.jpg" width="200" height="100" name="ccc"/>
    
    <input type=‘button‘ value="chagne" onclick="changeImage()"/>
    
    <script>
        function changeImage()
        {
            var img_name = prompt("please enter your image name:","");    
            var img_str = "window.document."+img_name;
            var img_obj = eval(img_str);
            if(img_obj)
                img_obj.src = "../image/desktop/"+parseInt(Math.random()*27+1)+".jpg";
        }
        
        
    </script>
</body>
</html>

 

        var json = ‘{"response":"success","level":"ok"}‘;
        //result = eval(json);//eval会将{}当做语句块来处理,而不是对象
        result = eval(‘(‘+json+‘)‘);
        alert(result);

         result = (new Function("","return"+json))();
         alert(result);

 

 

eval函数应用

标签:

原文地址:http://www.cnblogs.com/pmx-pmx/p/5051686.html

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!