如何在网页实现自己的划词搜索
近来在网上看到很多,网页里划词搜索的博文。大致都是划词实现GOOGLE或其他搜索引擎的搜索,极大的方便了客户端使用。
但是实现搜索自己的数据库几乎没有论述的。本人略加改动就实现了这个功能。
下面是实现搜索引擎搜索的例子!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<SCRIPT language=javascript>
<!--
document.body.οnlοad=adddiv;
document.οnmοusedοwn=recordobj;
document.οndblclick=dbclick;
document.οnmοuseup=showselect;
var starobj,isdb=false,allow=true;
function isallow()
{
if(allow){
allow=false;
alert('The function is closed!');
}
else{
allow=true;
alert('The function is opend!');
}
}
function dbclick()
{
isdb=true;
}
function recordobj()
{
starobj=event.srcElement;
}
function showselect() {
var str="";
if(event.srcElement.tagName!="A"&&event.srcElement.tagName!="INPUT"&&event.srcElement==starobj&&!isdb&&allow)
{
var oText=document.selection.createRange();
if(oText.text.length>0)
{
str=oText.text;
oText.text="BuB"+oText.text+"EuE";
}
oText.select();
event.srcElement.innerHTML=event.srcElement.innerHTML.replace("BuB","<u style='FONT-WEIGHT: bold;COLOR: #ff3366'>").replace("EuE","</u>");
}
searchgoogle(str)
isdb=false;
}
function searchgoogle(str)
{
var obj=document.getElementById("searchgoogle");
if(str.length>0)
{
obj.style.display="block";
obj.style.position="absolute";
obj.style.zindex=999;
obj.style.posTop=document.body.scrollTop+event.y-25;
obj.style.posLeft=document.body.scrollLeft+event.x+5;
obj.style.widht=80;
obj.innerHTML="<a target=_blank href=http://www.google.com/search?ie=UTF-8&oe=UTF-8&q="+str+" style='BORDER-RIGHT: royalblue thin solid; BORDER-TOP: royalblue thin solid; FONT-WEIGHT: bold; BORDER-LEFT: royalblue thin solid; CLIP: rect(auto auto auto auto); COLOR: #ffffff; BORDER-BOTTOM: royalblue thin solid; BACKGROUND-COLOR: inactivecaption; TEXT-DECORATION: none'>Search It!</a>";
}
else
{
obj.style.display="none";
}
}
function adddiv()
{
var mobj = document.createElement("div");
mobj.id="searchgoogle";
document.body.appendChild(mobj);
}
//-->
</SCRIPT>
<INPUT type="button" οnclick="isallow()" value="关闭/打开划词功能">
<p>云计算互联网新技术2010刘铭QQ403244600新文章李开复搜狗划词搜索词汇请选取</p>
</body>
</html>
那么大家 改动 一下 把href=http://www.google.com/s.........
这段代码后的地址改成项目里的地址,获取到查询关键字,就可以传递给您开发的程序做搜索,搜索到的结果用新界面显示出来就行了。
深入的应用也可以就此展开,只要获取到搜索关键字,就能发挥编程技巧,编辑您所需要的程序。