<html> <head> <title>劃詞搜索</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> <body> <SCRIPT language=Javascript> document.body.οnlοad=adddiv; //頁面加載時動態創建div document.οnmοusedοwn=recordobj; //鼠標按下時觸發的事件 document.οndblclick=dbclick; //窗體雙擊時觸發的事件 document.οnmοuseup=showselect; //鼠標彈起時觸發的事件 var eventObj; var isDouble=false; var allow=true; //是否啓用劃詞搜索 function isallow() { if(allow){ allow=false; alert('已經關閉'); } else{ allow=true; alert('已經打開'); } } function dbclick() //雙擊事件 { isDouble=true; } function recordobj() //當前操作對象 { eventObj=event.srcElement; } function showselect() { var str=""; if(event.srcElement.tagName!="A"&&event.srcElement.tagName!="INPUT"&&event.srcElement==eventObj&&!isDouble&&allow) { var oText=document.selection.createRange();//獲取選擇文本 if(oText.text.length>0) //如果文本存在 { str=oText.text; oText.text="begin"+oText.text+"end"; //包裝被選中的文本 } oText.select(); //實現選擇 //設置選中文本的樣式-帶下劃線-變顏色 event.srcElement.innerHTML=event.srcElement.innerHTML.replace("begin","<u style='FONT-WEIGHT: bold;COLOR: #ff3366'>").replace("end","</u>"); } googleDiv(str) //實現選定文本的搜索 isDouble=false; } function googleDiv(str) { var obj=document.getElementById("googleDiv");//獲取動態添加的div if(str.length>0) { obj.style.display="block"; //顯示div obj.style.position="absolute"; //設置div絕對位置 obj.style.zindex=999; obj.style.posTop=document.body.scrollTop+event.y-25;//div的Y座標 obj.style.posLeft=document.body.scrollLeft+event.x+5;//div的X座標 obj.style.widht=80; //div的寬度 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'>搜索</a>"; } else { obj.style.display="none"; } } function adddiv()//動態添加div標籤 { var mobj = document.createElement("div"); //創建div標籤 mobj.id="googleDiv"; //設置div標籤的id document.body.appendChild(mobj); //將div添加到窗體中 } </SCRIPT> <INPUT type="button" οnclick="isallow()" value="關閉/打開劃詞功能"> <p>This is Test ,please Select,it's will google</p> </body> </html>