js方法返回,如何取值?下面demo两种方法 new array 和 json 返回值 取值示例.
方法一: new array
1 <html> 2 <head> 3 <meta charset='UTF-8'> 4 <title>方法一: New Array</title> 5 </head> 6 <body> 7 <input type='button' onclick='getInfo()' value='测试按钮'/> 8 </body> 9 <script type='text/javascript'> 10 function getData(){ 11 return new Array('admin_one','Hello World One'); 12 //return ['Admin_One','Hello World One']; 13 } 14 function getInfo(){ 15 var data=getData(); 16 alert(data[0]); //admin_one 17 alert(getData()[1]); //Hello World One 18 } 19 </script> 20 </html>
结果打印:
方法二: json
1 <html> 2 <head> 3 <meta charset='UTF-8'> 4 <title>方法二: Josn</title> 5 </head> 6 <body> 7 <input type='button' onclick='getInfo()' value='测试按钮'/> 8 </body> 9 <script type='text/javascript'> 10 function getData(){ 11 return {'name':'admin_two','content':'Hello World Two'}; 12 } 13 function getInfo() 14 { 15 var data=getData(); 16 alert(data.name); //取值方法一: admin_two 17 alert(getData().content); //取值方法一:Hello World Two 18 19 alert(data['name']); //取值方法二:admin_two 20 alert(getData()['content']); //取值方法二:Hello World Two 21 22 } 23 </script> 24 </html>
结果打印: