RSS
热门关键字:  css  jquery  select input  asp jquery  用户注册
当前位置 :| 主页>JQuery教程>

一个用Jquery 自己写的一个全文检索高亮的一个JS函数

来源:cnjquery 作者:kan831019 时间:2008-10-07 Tag:全文检索   高亮   点击:
只需要在页面加载后调用即可,如:
<SCRIPT language=javascript>
$(document).ready(function(){
differ_text('a','keyword','red',true);
});
</SCRIPT>

这样就会把A标签中有GET中keyword内容全部变为红色并加粗,省去了在PHP中遍数组的麻烦
//--------------------------------------
//全文检索高亮
// 根据查询的关键字去页面中所有指定的元素中去
// 查找符合关键字内容的值,并将该值加了COLOR的属性以及加粗的属性
//
//id:需要查找的HTML元素名称
//key:GET上面查询关键字名称
//color:高亮的颜色
//b:是否加粗
//--------------------------------------

function differ_text(id,key,color,b)
{
var text;
text =location.search.substring(1);
text = text.replace(/&/ig,",");
text = text.replace(/=/ig,",");
text = text.split(",");

for(i=0;i<text.length;i++)
{
if(text == key && text[(i+1)] != undefined)
{
text = decodeURI(text[(i+1)]);
text = text.replace(/\\/ig,'\\');
text = text.replace(/\'/ig,'\\\'');
text = text.replace(/\"/ig,'\\\"');
break;
}
}
if(text == null || text == '' || text == undefined)
{
return false;
}

if(text.length > 0)
{
$(id+"nly-child:contains('"+text+"')").each(function (i){

var b = $(this).html();

var text_a = text.fontcolor(color);

text_a = b == true ? text_a.bold () :text_a;

var re = eval('/'+text+'/i');
$(this).html(b.replace(re,text_a));
});
}
}

最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册