服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

javascript实现划词标记+划词搜索功能

以下是引用片段:
<!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.onload=adddiv;
document.onmousedown=recordobj;
document.ondblclick=dbclick;
document.onmouseup=showselect;
var starobj,isdb=false,allow=true;
function isallow()
{
if(allow){
allow=false;
alert('is closed');
}
else{
allow=true;
alert('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" onclick="isallow()" value="关闭/打开划词功能">
<p>qrasfdasfasfdasfsafasdfsafsafasdfasdfasd</p>
</body>
</html>

扫描关注微信公众号