1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > javascript实现划词搜索功能(兼容IE firefox opera)

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

时间:2021-06-21 18:45:27

相关推荐

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

网上流传的Js版划词搜索代码浏览器兼容上有问题,经过两天的改写终于做出可兼容非IE的版本.

JS代码

默认搜索引擎为:google

转自: js中国

/thread-2-1-1.html

<!--

if(navigator.appName=="MicrosoftInternetExplorer"){

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;

}

///

varstarobj,

isdb=false,

allow=true;

///

functionisallow()

{

if(allow){

allow=false;

alert('isclosed');

}

else{

allow=true;

alert('isopend');

}

}

////

functiondbclick()

{

isdb=true;

}

/////

//functionrecordobj(evt)

//{

//starobj=event.srcElement;//IE

//starobj=evt.target

//}

functionrecordobj(evt){

evt=(evt)?evt:((window.event)?window.event:"")

if(evt){

starobj=(evt.target)?evt.target:evt.srcElement

}

//starobj=evt.srcElement;

}

////

functionshowselect(evt){

//test

varobj;

varstrlen;

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

varstr="";

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

{

//varoText=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;

}

//

functionsearchgoogle(str,evt)

{

varobj=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=="MicrosoftInternetExplorer")

{

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="<atarget=_blankhref=/search?ie=UTF-8&oe=UTF-8&q="+encodeURI(str)+"style='BORDER-RIGHT:royalbluethinsolid;BORDER-TOP:royalbluethinsolid;FONT-WEIGHT:bold;BORDER-LEFT:royalbluethinsolid;CLIP:rect(autoautoautoauto);COLOR:#ffffff;BORDER-BOTTOM:royalbluethinsolid;BACKGROUND-COLOR:inactivecaption;TEXT-DECORATION:none'>SearchIt!</a>";

}

else

{

obj.style.display="none";

}

}

///

functionadddiv()

{

varmobj=document.createElement("div");

mobj.id="searchgoogle";

document.body.appendChild(mobj);

}

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。