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

it2022-05-09  35

、 网上流传的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

相关资源:javascript小组件 原生table排序表格脚本(兼容ie firefox opera chrome)

最新回复(0)