javascript 自动标记来自搜索结果页的关键字
作者:bea
<html> <head> <style> /*定义样式*/ .hilite1{background:#69c;} </style> <script language="javascript" type="text/javascript"> Hilite = { exact: true, max_nodes: 1000, style_name: 'hilite', style_name_suffix: true }
<html>
<head>
<style>
/*定义样式*/
.hilite1{background:#69c;}
</style>
<script language="javascript" type="text/javascript">
Hilite = {
exact: true,
max_nodes: 1000,
style_name: 'hilite',
style_name_suffix: true
};
Hilite.hiliteElement = function (elm, query) {
if (!query || elm.childNodes.length == 0)
return;
query = query.split(/[s,+.]+/);
var qre = new Array();
for (var i = 0; i < query.length; i++) {
query[i] = query[i].toLowerCase();
if (Hilite.exact)
qre.push('\b' + query[i] + '\b');
else
qre.push(query[i]);
}
qre = new RegExp(qre.join("|"), "i");
var stylemapper = {};
for (var i = 0; i < query.length; i++) {
if (Hilite.style_name_suffix)
stylemapper[query[i]] = Hilite.style_name + (i + 1);
else
stylemapper[query[i]] = Hilite.style_name;
}
var textproc = function (node) {
var match = qre.exec(node.data);
if (match) {
var val = match[0];
var k = '';
var node2 = node.splitText(match.index);
var node3 = node2.splitText(val.length);
var span = node.ownerDocument.createElement('SPAN');
node.parentNode.replaceChild(span, node2);
span.className = stylemapper[val.toLowerCase()];
span.appendChild(node2);
return span;
} else {
return node;
}
};
Hilite.walkElements(elm.childNodes[0], 1, textproc);
};
Hilite.walkElements = function (node, depth, textproc) {
var skipre = /^(script|style|textarea)/i;
var count = 0;
while (node && depth > 0) {
count++;
if (count >= Hilite.max_nodes) {
var handler = function () {
Hilite.walkElements(node, depth, textproc);
};
setTimeout(handler, 50);
return;
}
if (node.nodeType == 1) {
if (!skipre.test(node.tagName) && node.childNodes.length > 0) {
node = node.childNodes[0];
depth++;
continue;
}
} else if (node.nodeType == 3) {
node = textproc(node);
}
if (node.nextSibling) {
node = node.nextSibling;
} else {
while (depth > 0) {
node = node.parentNode;
depth--;
if (node.nextSibling) {
node = node.nextSibling;
break;
}
}
}
}
};
Hilite.autoForSeachEngine = function () {
var re = new RegExp('[\?&](q|keyword|qk|wd|p)=([^&]+)','ig');
var ref = document.referrer || 'http://search.yahoo.com/search?p=javascript&toggle=1&cop=mss&ei=UTF-8&fr=yfp-t-701';
addLoadEvent(function () {
var match = re.exec(ref);
if (match) {
var query = decodeURIComponent(match[2]);
Hilite.hiliteElement(document.getElementById('divMain'), query);
}
});
};
function addLoadEvent(f) {
var old = window.onload;
if (typeof old != 'function') window.onload = f;
else { window.onload = function () { old(); f(); }; }
}
Hilite.autoForSeachEngine();
</script>
</head>
<body>
<div id="divMain">
我是test,我要被highlight。我们是javascript,我们是javascript
</div>
</body>
</html>
[Ctrl+A 全选 注:
如需引入外部Js需刷新才能执行]
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- Jquery 常用方法经典总结
- javascript 匿名函数的理解(透彻版)
- JavaScript 学习笔记(十五)
- Extjs 几个方法的讨论
- JavaScript 判断判断某个对象是Object还是一个Array
- 图像替换新技术 状态域方法
- javascript innerText和innerHtml应用
- js 右键菜单,支持不同对象不同菜单(兼容IE、Firefox)
- 使用JavaScript库还是自己写代码?
- js或css实现滚动广告的几种方案
- jquery中的sortable排序之后的保存状态的解决方法
- javascript面向对象的方式实现的弹出层效果代码
- js提示信息jtip封装代码,可以是图片或文章
- jquery 常用操作方法
- jquery 经典动画菜单效果代码
- 使用JQuery进行跨域请求
- JavaScript 使用简略语法创建对象的代码
- javascript 的Document属性和方法集合
- 起点页面传值js,有空研究学习下