大唐+javascript 静态化简单实例

2012 年 11 月 26 日6720

大唐+javascript 静态化简单实例

发布时间:2012-10-19 12:07:05来源:评论:】

1. 大唐生成js文件:<?大唐a=array(array(&lsquo;title&rsquo;=>&rsquo;本地新闻1&prime;,ln=>&rsquo;#&#39;),array(&lsquo;title&rsquo;=>&rsquo;本地新闻2&prime;,ln=>&rsquo;#&#39;),array(&lsquo;title&rsquo;...

   

1. 大唐生成js文件:

<?大唐

a=array(

array(‘title’=>’本地新闻1′,ln=>’#'),

array(‘title’=>’本地新闻2′,ln=>’#'),

array(‘title’=>’本地新闻3′,ln=>’#')

);//实际操作中可通过数据库得到记录集数组

news=’ var a=['; //把该数组转化为javascrīpt形式

for(i=0;i<count(a);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 文件

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://http://www.zjjv.com///TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://http://www.zjjv.com///1999/xhtml”>

<head>

<meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″ />

<title>无标题文档</title>

</head>

<body>

<div id=”news” style=”display:none” > //定义显示的格式,先通过display:none 隐藏

<table border=”1″ bordercolor=”#FF0000″>

<tr>

<td><a href=”link”>title</a></td>

</tr>

</table>

</div>

<scrīpt language=”javascrīpt” src=”file.js”> //引用js

</scrīpt>

<scrīpt language=”javascrīpt” >

news_obj=document.getElementById(‘news’);

e=news_obj.innerHTML;

re=/\title/g; //正则,匹配title

re2=/\link/g; // 匹配link

for(i=0;i<a.length;i++) {

tmp=e.replace(re,a[i].title); //用a[i].title代替取出的title

tmp=tmp.replace(re2,a[i].ln);

document.write(tmp); //显示文本

}

</scrīpt>

<table width=”200″ border=”1″>

</table>

</body>

</html>

(责任编辑:)

本文关键字:

上一篇:PHP实例:PHP通过参数来生成MYSQL语句类

下一篇:

分享到:

最新资讯

热门内容

0 0