数据源写成json格式 <html> <head> <style> a.lk { margin-right:10px; } </style> <script> window.onload=function(){ var json={"aa":"http://aa.htm","bb":"http://bb.htm","cc":"http://cc.htm"}; var div = document.getElementById("container"); for(var i in json) { var a = document.createElement("a"); a.className = "lk"; a.innerText = i; a.href=json[i]; a.target="blank"; div.appendChild(a);
<html>
<head>
<style>
a.lk
{
margin-right:10px;
}
</style>
<script>
window.onload=function(){
var json={"aa":"http://aa.htm","bb":"http://bb.htm","cc":"http://cc.htm"};
var div = document.getElementById("container");
for(var i in json)
{
var a = document.createElement("a");
a.className = "lk";
a.innerText = i;
a.href=json[i];
a.target="blank";
div.appendChild(a);
}
}
</script>
</head>
<body>
<div id="container"></div>
</body>
</html>
不过我希望的是将下面含有网址的字符自动变换成加上超级链接,这替换和查找有没什么好方法呢. 来路网址:http://zhidao.baidu.com/63164.html
访问时间:[2009-02-03 12:59:58]
访问网址:http://www.cc.com/ 访问时间:[2009-02-03 13:01:05]
访问网址:http://www.cc.com/he.asp 访问时间:[2009-02-03 13:01:18]
访问网址:http://www.ss.com/td.asp 访问时间:[2009-02-03 13:01:22]
访问网址:http://www.vv.com/index.asp 访问时间:[2009-02-03 13:06:25]
访问网址:http://www.ee.com/lx.asp 访问时间:[2009-02-03 13:06:31]
访问网址:http://www.nn.com/index.asp
<head>
<style>
a.lk
{
margin-right:10px;
}
</style>
<script>
window.onload=function(){
var div = document.getElementById("container");
var s=div.innerHTML;
var re = /(http:\/\/[\w.\/]+)/gi;
div.innerHTML=s.replace(re,"<a href='$1' target='_blank'>$1</a>");
}
</script>
</head>
<body>
<div id="container">
来路网址:http://zhidao.baidu.com/63164.html <br />
访问时间:[2009-02-03 12:59:58]
访问网址:http://www.sina.com/ <br />访问时间:[2009-02-03 13:01:05]
访问网址:http://www.sina.com/he.asp <br />访问时间:[2009-02-03 13:01:18]
访问网址:http://www.sina.com/td.asp <br />访问时间:[2009-02-03 13:01:22]
访问网址:http://www.sina.com/index.asp <br />访问时间:[2009-02-03 13:06:25]
访问网址:http://www.sina.com/lx.asp <br />访问时间:[2009-02-03 13:06:31]
访问网址:http://www.sina.com/index.asp
</div>
</body>
</html>
再请教一下:好像下面的如何避免错误呢:
<IMG height=20 src='http://127.0.0.1/qqkf/img/aa.gif' width=20>
<head>
<style>
a.lk
{
margin-right:10px;
}
</style>
<script>
window.onload=function(){
var div = document.getElementById("container");
var s=div.innerHTML;
var re = /(http:\/\/[\w.\/]+)(?!['"]*)/gi;
div.innerHTML=s.replace(re,"<a href='$1' target='_blank'>$1</a>");
}
</script>
</head>
<body>
<div id="container">
来路网址:http://zhidao.baidu.com/63164.html <br />
访问时间:[2009-02-03 12:59:58]
访问网址:http://www.sina.com/<br/>访问时间:[2009-02-03 13:01:05]
访问网址:http://www.sina.com/he.asp<br/>访问时间:[2009-02-03 13:01:18]
访问网址:http://www.sina.com/td.asp<br/>访问时间:[2009-02-03 13:01:22]
访问网址:http://www.sina.com/index.asp<br/>访问时间:[2009-02-03 13:06:25]
访问网址:http://www.sina.com/lx.asp<br/>访问时间:[2009-02-03 13:06:31]
访问网址:http://www.sina.com/index.asp
<img height=20 src='http://127.0.0.1/qqkf/img/aa.gif' width=20>
</div>
</body>
</html>
CutBug你真棒.
CSDN真好.