JavaScript和PHP可以相互巢狀,前提是JavaScript寫在以.php為字尾的檔案裡面。
(1) JavaScript巢狀PHP:
直接在JavaScript裡面寫入PHP語句,如:
<script type="text/javascript"> BUI.use('bui/grid',function (Grid) { var data = [//表單內容資訊 <?php foreach($slice_rows as $key=>$val){ echo "{id:'".$val['id']."',imgpath:'".$val['imgpath']."',introduction:'".$val['introduction']. "',linkpath:'".$val['linkpath']."',date:'".$val['date']."'},"; } echo "{id:'1112',imgpath:'李四',introduction:'this is a test',linkpath:'#',date:'date2'}"; ?> ], grid = new Grid.SimpleGrid({ render : '#grid', //顯示Grid到此處 width : 950, //設定寬度 columns : [//表單頭部資訊 {title:'編號',dataIndex:'id',width:80}, {title:'圖片路徑',dataIndex:'imgpath',width:100}, {title:'簡介',dataIndex:'introduction',width:200}, {title:'連結地址',dataIndex:'linkpath',width:100}, {title:'上傳日期',dataIndex:'date',width:100} ] }); grid.render(); grid.showData(data); }); </script>
(2)PHP巢狀JavaScript:
<?php echo '<script type="text/javascript">'; echo " BUI.use('bui/grid',function (Grid) {"; //表單的資料內容 echo 'var data = ['; foreach($slice_rows as $key=>$val){ echo "{id:'".$val['id']."',imgpath:'".$val['imgpath']."',introduction:'".$val['introduction']. "',linkpath:'".$val['linkpath']."',date:'".$val['date']."'},"; } echo "{id:'1112',imgpath:'李四',introduction:'1349622209547',linkpath:'#',date:'date2'}"; echo '],'; //表單的頭部資訊 echo " grid = new Grid.SimpleGrid({ render : '#grid', //顯示Grid到此處 width : 950, //設定寬度 columns : [ {title:'編號',dataIndex:'id',width:80}, {title:'圖片路徑',dataIndex:'imgpath',width:100}, {title:'簡介',dataIndex:'introduction',width:200}, {title:'連結地址',dataIndex:'linkpath',width:100}, {title:'上傳日期',dataIndex:'date',width:100} ] }); "; echo ' grid.render(); grid.showData(data); }); '; //echo 'alert("this is a test")'; echo '</script>'; ?>以上就是js怎麼跟php結合使用的詳細內容,更多請關注TW511.COM其它相關文章!