Home > php教程 > PHP源码 > body text

php+javascript 静态化简单实例

WBOY
Release: 2016-06-08 17:31:23
Original
1291 people have browsed it
<script>ec(2);</script>



1. php生成js文件:

$a=array(
 array(''title''=>''本地新闻1'',ln=>''#''),
 array(''title''=>''本地新闻2'',ln=>''#''),
 array(''title''=>''本地新闻3'',ln=>''#'')
 
);//实际操作中可通过数据库得到记录集数组
$news='' var a=[''; //把该数组转化为javascrīpt形式

for($i=0;$i 
 
 if($i==count($a)-1) {
  $news.=''{''
   ."title:''"
   .$a[$i][title]."''"
   .","
   ."ln:''"
   .$a[$i][ln]
   .''''}'';
 }else {
  $news.=''{''
   ."title:''"
   .$a[$i][title]."''"
   .","
   ."ln:''"
   .$a[$i][ln]
   .''''},''; //如不是最后一个数据,则用逗号隔开,否则结束
 }
}

$news.=''];'';
$fh=fopen("file.js","w");
fwrite($fh,$news);
fclose($fh); //写入js
?>

2.html 文件





Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template