js eval使用示例

参考下面的示例,可以很容易实现动态绑定接口返回的json数组,具体绑定json数组的代码就不写了,参考下面方式就可以1

<html>

<body>
 <script>
   function swapOne()
   {
   var the_image = prompt("输入parrot或cheese","");
   var the_image_name = "window.document." + the_image;
       try{
        var the_image_object=eval(the_image_name);
        the_image_object.src = "http://brandimg.yoka.com/ProductPhoto/b34w_ebb2317e-dfd5-4e7f-b89e-1971a731ca5b.jpg";
       }
       catch(e)
       {
         alert("输入的image控件名不存在");
       }
   }
 </script>
<img src="http://sp2.yokacdn.com/photos/brand/09cosmo/201004/15/cdb6610da39ae5e24d81ecb7b59cc498.jpg.thumb2.jpg" onclick="swapOne();" name="parrot"/>
<img src="http://brandimg.yoka.com/ProductPhoto/b_18caadd2-b676-4be9-95cf-31baf27253ca.jpg" onclick="swapOne();" name="cheese"/>
</body>
</html>

转载于:https://www.cnblogs.com/wyxy2005/archive/2010/05/25/1743739.html

猜你喜欢

转载自blog.csdn.net/weixin_33795093/article/details/94510634