基于jquery的頁(yè)面劃詞搜索JS
更新時(shí)間:2010年09月14日 01:34:31 作者:
這個(gè)腳本是網(wǎng)上的代碼修改的,兼容各主流瀏覽器,優(yōu)化頁(yè)面異步加載的文字的檢索提示框定位,根據(jù)選擇文字位置與屏幕尺寸計(jì)算檢索提示框定位。
基于Jquery修改,以下為全部腳本
//劃詞搜索
var GLS = {};
GLS.startObj = null;
GLS.isdb = false;
GLS.allow = true;
GLS.isallow = function() {
if (GLS.allow) {
GLS.allow = false;
alert('Google搜索已關(guān)閉');
}
else {
GLS.allow = true;
alert('Google搜索已打開(kāi)');
}
};
GLS.dblclick = function() {
GLS.isdb = true;
};
GLS.mousedown = function(evt) {
evt = (evt) ? evt : ((window.event) ? window.event : "");
if (evt) {
GLS.startObj = (evt.target) ? evt.target : evt.srcElement;
}
};
GLS.mouseup = function(evt) {
var obj;
var strlen;
evt = (evt) ? evt : ((window.event) ? window.event : "");
if (evt) {
obj = (evt.target) ? evt.target : evt.srcElement;
strlen = window.getSelection ? window.getSelection().toString() : document.selection.createRange().text;
}
var str = "";
if (obj.tagName != "A" && obj.tagName != "INPUT" && obj == GLS.startObj && !GLS.isdb && GLS.allow) {
if (strlen.length > 0) {
str = strlen;
}
}
GLS.search(str, evt);
GLS.isdb = false;
};
GLS.search = function(str, evt) {
var obj = $("#GLSearch");
var sDivWidth = 88; //檢索框“Google搜索”的寬度
if (str.toString().length > 0) {
var windowWidth; //窗口的寬
//取得窗口的寬
if (self.innerWidth) {
windowWidth = self.innerWidth;
} else if (document.documentElement && document.documentElement.clientWidth) {
windowWidth = document.documentElement.clientWidth;
} else if (document.body) {
windowWidth = document.body.clientWidth;
}
obj.css({ 'display': 'block', 'position': 'absolute', 'zindex': '10000' });
var rX, rX, wT;
if ($.browser.msie) {
wT = (evt.clientX + sDivWidth) - windowWidth;
rY = document.documentElement.scrollTop + evt.clientY;
rX = document.documentElement.scrollLeft + evt.clientX;
rY = (evt.clientY < 25) ? rY + 5 : rY - 25;
rX = (wT > 0) ? rX - wT : rX + 5;
}
else {
var sT = (document.documentElement.scrollTop > 0) ? document.documentElement.scrollTop : document.body.scrollTop;
wT = (evt.pageX + sDivWidth) - windowWidth;
rY = ((evt.pageY - sT) < 25) ? evt.pageY + 5 : evt.pageY - 25;
rX = (wT > 0) ? evt.pageX - wT : evt.pageX + 5;
}
obj.css("top", rY);
obj.css("left", rX);
obj.html("<a class='GL_s' target='_blank' + encodeURIComponent(str) + "'>Google搜索</a>");
}
else {
obj.css("display", "none");
}
};
//頁(yè)面加載
$(document).ready(function() {
$(document.body).append("<div id='GLSearch'></div>");
$(document).mousedown(GLS.mousedown).dblclick(GLS.dblclick).mouseup(GLS.mouseup);
});------------附上CSS/*search*/
a.GL_s{width:68px;height:20px;display:block;border:solid 1px #2b7ab7;line-height:20px;padding-left:18px;color:#084f10;font-size:12px;font-weight:bold;background:url(search.gif) no-repeat left;text-decoration:none;}
a.GL_s:link{filter:alpha(opacity=71);-moz-opacity:0.7;opacity: 0.7;}
a.GL_s:hover{filter:alpha(opacity=100);-moz-opacity:1.0;opacity: 1.0;}----------search.gif圖片
復(fù)制代碼 代碼如下:
//劃詞搜索
var GLS = {};
GLS.startObj = null;
GLS.isdb = false;
GLS.allow = true;
GLS.isallow = function() {
if (GLS.allow) {
GLS.allow = false;
alert('Google搜索已關(guān)閉');
}
else {
GLS.allow = true;
alert('Google搜索已打開(kāi)');
}
};
GLS.dblclick = function() {
GLS.isdb = true;
};
GLS.mousedown = function(evt) {
evt = (evt) ? evt : ((window.event) ? window.event : "");
if (evt) {
GLS.startObj = (evt.target) ? evt.target : evt.srcElement;
}
};
GLS.mouseup = function(evt) {
var obj;
var strlen;
evt = (evt) ? evt : ((window.event) ? window.event : "");
if (evt) {
obj = (evt.target) ? evt.target : evt.srcElement;
strlen = window.getSelection ? window.getSelection().toString() : document.selection.createRange().text;
}
var str = "";
if (obj.tagName != "A" && obj.tagName != "INPUT" && obj == GLS.startObj && !GLS.isdb && GLS.allow) {
if (strlen.length > 0) {
str = strlen;
}
}
GLS.search(str, evt);
GLS.isdb = false;
};
GLS.search = function(str, evt) {
var obj = $("#GLSearch");
var sDivWidth = 88; //檢索框“Google搜索”的寬度
if (str.toString().length > 0) {
var windowWidth; //窗口的寬
//取得窗口的寬
if (self.innerWidth) {
windowWidth = self.innerWidth;
} else if (document.documentElement && document.documentElement.clientWidth) {
windowWidth = document.documentElement.clientWidth;
} else if (document.body) {
windowWidth = document.body.clientWidth;
}
obj.css({ 'display': 'block', 'position': 'absolute', 'zindex': '10000' });
var rX, rX, wT;
if ($.browser.msie) {
wT = (evt.clientX + sDivWidth) - windowWidth;
rY = document.documentElement.scrollTop + evt.clientY;
rX = document.documentElement.scrollLeft + evt.clientX;
rY = (evt.clientY < 25) ? rY + 5 : rY - 25;
rX = (wT > 0) ? rX - wT : rX + 5;
}
else {
var sT = (document.documentElement.scrollTop > 0) ? document.documentElement.scrollTop : document.body.scrollTop;
wT = (evt.pageX + sDivWidth) - windowWidth;
rY = ((evt.pageY - sT) < 25) ? evt.pageY + 5 : evt.pageY - 25;
rX = (wT > 0) ? evt.pageX - wT : evt.pageX + 5;
}
obj.css("top", rY);
obj.css("left", rX);
obj.html("<a class='GL_s' target='_blank' + encodeURIComponent(str) + "'>Google搜索</a>");
}
else {
obj.css("display", "none");
}
};
//頁(yè)面加載
$(document).ready(function() {
$(document.body).append("<div id='GLSearch'></div>");
$(document).mousedown(GLS.mousedown).dblclick(GLS.dblclick).mouseup(GLS.mouseup);
});------------附上CSS/*search*/
a.GL_s{width:68px;height:20px;display:block;border:solid 1px #2b7ab7;line-height:20px;padding-left:18px;color:#084f10;font-size:12px;font-weight:bold;background:url(search.gif) no-repeat left;text-decoration:none;}
a.GL_s:link{filter:alpha(opacity=71);-moz-opacity:0.7;opacity: 0.7;}
a.GL_s:hover{filter:alpha(opacity=100);-moz-opacity:1.0;opacity: 1.0;}----------search.gif圖片

您可能感興趣的文章:
- jquery實(shí)現(xiàn)靜態(tài)搜索功能(可輸入搜索文字)
- jQuery實(shí)現(xiàn)動(dòng)態(tài)文字搜索功能
- JS實(shí)現(xiàn)搜索框文字可刪除功能
- javascript搜索框點(diǎn)擊文字消失失焦時(shí)文本出現(xiàn)
- 利用jQuery實(shí)現(xiàn)可輸入搜索文字的下拉框
- JavaScript前端頁(yè)面搜索功能案例【基于jQuery】
- jQuery Jsonp跨域模擬搜索引擎
- jquery中用jsonp實(shí)現(xiàn)搜索框功能
- JQuery+JS實(shí)現(xiàn)仿百度搜索結(jié)果中關(guān)鍵字變色效果
- jquery+json實(shí)現(xiàn)的搜索加分頁(yè)效果
- jQuery單頁(yè)面文字搜索插件jquery.fullsearch.js的使用方法
相關(guān)文章
基于jquery實(shí)現(xiàn)的文字淡入淡出效果
這篇文章介紹了jquery實(shí)現(xiàn)的文字淡入淡出效果實(shí)例,有需要的朋友可以參考一下2013-11-11jQuery內(nèi)部原理和實(shí)現(xiàn)方式淺析
這篇文章主要介紹了jQuery內(nèi)部原理和實(shí)現(xiàn)方式淺析,本文試圖從整體來(lái)闡述一下jQuery的內(nèi)部實(shí)現(xiàn),需要的朋友可以參考下2015-02-02jQuery實(shí)現(xiàn)表格隔行及滑動(dòng),點(diǎn)擊時(shí)變色的方法【測(cè)試可用】
這篇文章主要介紹了jQuery實(shí)現(xiàn)表格隔行及滑動(dòng),點(diǎn)擊時(shí)變色的方法,可實(shí)現(xiàn)表格隔行變色以及鼠標(biāo)滑過(guò)與點(diǎn)擊時(shí)變色的功能,涉及jQuery響應(yīng)鼠標(biāo)事件及頁(yè)面元素樣式動(dòng)態(tài)改變的相關(guān)技巧,需要的朋友可以參考下2016-08-08jQuery Ajax自定義分頁(yè)組件(jquery.loehpagerv1.0)實(shí)例詳解
這篇文章主要介紹了jQuery Ajax自定義分頁(yè)組件(jquery.loehpagerv1.0)實(shí)例詳解,需要的朋友可以參考下2017-05-05淺談jQuery.easyui的datebox格式化時(shí)間
這篇文章主要介紹了淺談jQuery.easyui的datebox格式化時(shí)間的方法,需要的朋友可以參考下2015-06-06