标签:
1.
<?php
$arr = array("大理","昆明");
$json = json_encode($arr);
?>
<script>
var jsarr = <?php echo $json;?>;
alert(jsarr[0]);
</script>
2.
<!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>
<title>第一php网提供的教程--向JS传递PHP数组</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"/></script>
<script language=javascript>
function get_php_arr(arr){
alert(eval(arr));
}
</script>
</head>
<body>
<p>作者:遥远的期待 QQ:15624575</p>
<p>个人网站:www.phptogether.com</p>
<a href=‘javascript:get_php_arr("<?php echo json_encode(array(1,2,3))?>");‘>向js传递PHP数组</a>
</body>
</html>
3.
<!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>测试PHP和JavaScript交换参数</title>
</head>
<body>
<script type="text/javascript">
function getProfile(str)
{
var arr =eval(str);//或eval(str)
var s="";
for(var i=0; i<arr.length; i++)
{
s+="name:"+arr[i].name+",nick:"+arr[i].nick+":";
}
document.getElementById(‘TestName‘).innerHTML =s;
}
</script>
<body><div id="TestName"></div></body>
<script type="text/javascript" src="../TestJS.php"></script>
</body>
</html>
标签:
原文地址:http://www.cnblogs.com/maipaocai/p/5253014.html