添加关键词链接
网页中一些需要特别解释的关键词通常会以链接的形式呈现,这样可以方便用户通过链接的页面了解与关键字相关的信息。这种方式在新闻网站、BLOG、WIKI网站中应用得非常广泛。
实例:
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<!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>1</title>
<style type="text/css">
p {font-size: 11pt}
</style>
<script type="text/javascript">
function init(){
var p = document.getElementsByTagName("p");
for (var i=0; i<p.length; i++)
process(p[i]);
}
String.prototype.trim = function(){
var re = /^\s+|\s+$/g;
return this.replace(re,"");
};
var keywordInfo = {
关键词1: {url: "http://www.baidu.com"},
关键词2: {url: "http://www.baidu.com"},
关键词3: {url: "http://www.baidu.com"}
}
function buildRegex() {
var keys = [];
for (var p in keywordInfo)
keys.push(p);
var re = new RegExp(keys.join("|"),"gi");
return re;
}
function process(p) {
var text = p.innerHTML.trim();
var re = buildRegex();
var newText = text.replace(re,function(keyword) {
var replaceHTML = keyword;
var info = keywordInfo[keyword.toLowerCase()];
if (info)
replaceHTML = "<a href='" + info.url + "' >" + keyword + "</a>";
return replaceHTML;
});
p.innerHTML = newText;
}
</script>
</head>
<body onload="init()">
<p>fwefbfbghnmmukiuljhngfdnbe关键词1我回复翁我欧文机器哦啤酒股票么人关键词2我反而将我怕就怕sfwefwe关键词33</p>
</body>
</html>