现在的位置: 首页 > 综合 > 正文

javascript实现划词标记划词搜索功能修正版,兼容IE及FF

2018年01月27日 ⁄ 综合 ⁄ 共 2266字 ⁄ 字号 评论关闭

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>继续看</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>

<body>

<SCRIPT language=JavaScript>
<!--
if (navigator.appName=="Microsoft Internet Explorer") {
document.body.onload=adddiv; 
document.onmousedown=recordobj; 
document.ondblclick=dbclick; 
document.onmouseup=showselect;
} else
{
//firefox
window.onload=adddiv(); 
window.onmousedown=recordobj; 
window.ondblclick=dbclick(); 
window.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(evt)  { 

evt = (evt) ? evt : ((window.event) ? window.event : "")
if (evt) {
          starobj = (evt.target) ? evt.target : evt.srcElement
}

}

function showselect(evt) { 

var obj;
var strlen;
evt = (evt) ? evt : ((window.event) ? window.event : "")
if (evt) {
if (evt.target)
{
obj = evt.target;
strlen = window.getSelection().toString();
}
else
{
obj = event.srcElement;
strlen = document.selection.createRange().text;
}
}

var str=""; 
if(obj.tagName!="A"&&obj.tagName!="INPUT"&&obj==starobj&&!isdb&&allow) 

if(strlen.length>0)
{

 str=strlen; 
 

}

 
searchgoogle(str,evt); 

isdb=false; 
}

function searchgoogle(str,evt) 

var obj=document.getElementById("searchgoogle"); 

if(str.toString().length>0) 
{

obj.style.display="block"; 
obj.style.position="absolute"; 
obj.style.zindex=999;

if(navigator.appName=="Microsoft Internet Explorer")
{
obj.style.widht=80;

obj.style.posTop=document.body.scrollTop+event.y-25; 
obj.style.posLeft=document.body.scrollLeft+event.x+5;  
}
else
{
obj.style.width = "100px";
obj.style.top=evt.pageY-25;
obj.style.left=evt.pageX+5;
}
 
 
obj.innerHTML="<a target=_blank href=http://www.google.com/search?ie=UTF-8&oe=UTF-8&q="+encodeURI(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>继续看翻译宝</p>
</body>
</html>

抱歉!评论已关闭.