javascript实现划词搜索功能(兼容IE,firefox,opera)


网上流传的Js版划词搜索代码浏览器兼容上有问题,经过两天的改写终于做出可兼容非IE的版本.
JS代码
默认搜索引擎为:google
转自: js中国 http://www.javascriptcn.cn
http://javascriptxml.5d6d.com/thread-2-1-1.html
<!--  
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)  
//
{  
//
starobj=event.srcElement;   //IE
//
starobj=evt.target
//


function  recordobj(evt)  {  

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

// // 
function  showselect(evt) {  
// test
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;
}
}
// test
var  str = "" ;  
if (obj.tagName != " A " && obj.tagName != " INPUT " && obj == starobj &&! isdb && allow)  
{  
// var oText=document.selection.createRange();  //IE
//
alert(window.getSelection());
//
alert(window.getSelection().toString().length);
//
if(window.getSelection().length>0)  //错误写法
//
//
 


// if(window.getSelection().toString().length>0)
if (strlen.length > 0 )

// alert("ddd"+window.getSelection().boundingWidth());
//
alert("dddddddddddddddddddd000");
 str = strlen;  
// oText.text="BuB"+oText.text+"EuE";  
}  

// oText.select();  
//
window.getSelection.select()
}
// alert("自负" + str);
  
searchgoogle(str,evt);  

isdb
= false ;  
}
//   
function  searchgoogle(str,evt)  
{  
var  obj = document.getElementById( " searchgoogle " );  
// alert("长度"+str.toString().length);
if (str.toString().length > 0 )  
{
// alert("tongguo");  
//
alert(obj);
obj.style.display = " block " ;  
obj.style.position
= " absolute " ;  
obj.style.zindex
= 999
// alert("y" + evt.clientY + "X" + evt.clientX); 
//
//
alert(document.body.scrollTop.toString()); 
//
alert("new" + document.documentElement.scrollTop.toString());
//

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);  

}  
// -->  
直接在html页面中调用即可实现网页划词搜索功能.

转载于:https://www.cnblogs.com/wangxiang/articles/926462.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值