日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內(nèi)最全I(xiàn)T社區(qū)平臺 聯(lián)系我們 | 收藏本站
阿里云優(yōu)惠2
您當(dāng)前位置:首頁 > web前端 > jscript > JavaScript獲取文本框光標(biāo)的像素位置

JavaScript獲取文本框光標(biāo)的像素位置

來源:程序員人生   發(fā)布時間:2014-05-10 09:11:47 閱讀次數(shù):3467次
本文主要實(shí)現(xiàn)獲取textarea和input光標(biāo)的像素位置,即光標(biāo)的offsetLeft與offsetTop。可實(shí)現(xiàn)如下效果:

首先說明一下,在網(wǎng)上搜到的很多代碼是如何獲取輸入光標(biāo)位置的如下代碼是如何獲取光標(biāo)的的字符位置,如對于串“He|llo World!”返回的是光標(biāo)|前的字符數(shù)2,并不是光標(biāo)在頁面上的像素位置。當(dāng)然,這段代碼對于獲取光標(biāo)的像素位置能起到一定的輔助作用。

// 獲取光標(biāo)在文本框的位置
function _getFocus(elem) {
var index = 0;
if (document.selection) {// IE Support
elem.focus();
var Sel = document.selection.createRange();
if (elem.nodeName === 'TEXTAREA') {//textarea
var Sel2 = Sel.duplicate();
Sel2.moveToElementText(elem);
var index = -1;
while (Sel2.inRange(Sel)) {
Sel2.moveStart('character');
index++;
};
}
else if (elem.nodeName === 'INPUT') {// input
Sel.moveStart('character', -elem.value.length);
index = Sel.text.length;
}
}
else if (elem.selectionStart || elem.selectionStart == '0') { // Firefox support
index = elem.selectionStart;
}
return (index);
}
對于IE瀏覽器,通過下面的代碼1實(shí)現(xiàn)起來比較方便。
//代碼1
if (document.selection) {
elem.focus();
var Sel = document.selection.createRange();
return {
left: Sel.boundingLeft,
top: Sel.boundingTop,
bottom: Sel.boundingTop + Sel.boundingHeight
};
}

而對于firefox之類的瀏覽器則通過模擬來實(shí)現(xiàn),如下圖所示。首先通過拷貝輸入?yún)^(qū)域的樣式到一個div層(clone層),然后在此clone層之中的text子層添加光標(biāo)之前的字符,并在text子層之后添加focus層,focus層中包含字符“|”來模擬光標(biāo),進(jìn)而通過獲取focus層的偏移量即可獲得文本光標(biāo)的像素坐標(biāo)位置。

具體實(shí)現(xiàn)代碼如下:

var kingwolfofsky = {
/**
* 獲取輸入光標(biāo)在頁面中的坐標(biāo)
* @param {HTMLElement} 輸入框元素
* @return {Object} 返回left和top,bottom
*/
getInputPositon: function (elem) {
if (document.selection) { //IE Support
elem.focus();
var Sel = document.selection.createRange();
return {
left: Sel.boundingLeft,
top: Sel.boundingTop,
bottom: Sel.boundingTop + Sel.boundingHeight
};
} else {
var that = this;
var cloneDiv = '{$clone_div}', cloneLeft = '{$cloneLeft}', cloneFocus = '{$cloneFocus}', cloneRight = '{$cloneRight}';
var none = '<font style="white-space:pre-wrap;"> </font>';
var div = elem[cloneDiv] || document.createElement('div'), focus = elem[cloneFocus] || document.createElement('font');
var text = elem[cloneLeft] || document.createElement('font');
var offset = that._offset(elem), index = this._getFocus(elem), focusOffset = { left: 0, top: 0 };

if (!elem[cloneDiv]) {
elem[cloneDiv] = div, elem[cloneFocus] = focus;
elem[cloneLeft] = text;
div.appendChild(text);
div.appendChild(focus);
document.body.appendChild(div);
focus.innerHTML = '|';
focus.style.cssText = 'display:inline-block;width:0px;overflow:hidden;z-index:-100;word-wrap:break-word;word-break:break-all;';
div.className = this._cloneStyle(elem);
div.style.cssText = 'visibility:hidden;display:inline-block;position:absolute;z-index:-100;word-wrap:break-word;word-break:break-all;overflow:hidden;';
};
div.style.left = this._offset(elem).left + "px";
div.style.top = this._offset(elem).top + "px";
var strTmp = elem.value.substring(0, index).replace(/</g, '<').replace(/>/g, '>').replace(//g, '<br/>').replace(/s/g, none);
text.innerHTML = strTmp;

focus.style.display = 'inline-block';
try { focusOffset = this._offset(focus); } catch (e) { };
focus.style.display = 'none';
return {
left: focusOffset.left,
top: focusOffset.top,
bottom: focusOffset.bottom
};
}
},

// 克隆元素樣式并返回類
_cloneStyle: function (elem, cache) {
if (!cache && elem['${cloneName}']) return elem['${cloneName}'];
var className, name, rstyle = /^(number|string)$/;
var rname = /^(content|outline|outlineWidth)$/; //Opera: content; IE8:outline && outlineWidth
var cssText = [], sStyle = elem.style;

for (name in sStyle) {
if (!rname.test(name)) {
val = this._getStyle(elem, name);
if (val !== '' && rstyle.test(typeof val)) { // Firefox 4
name = name.replace(/([A-Z])/g, "-$1").toLowerCase();
cssText.push(name);
cssText.push(':');
cssText.push(val);
cssText.push(';');
};
};
};
cssText = cssText.join('');
elem['${cloneName}'] = className = 'clone' + (new Date).getTime();
this._addHeadStyle('.' + className + '{' + cssText + '}');
return className;
},

// 向頁頭插入樣式
_addHeadStyle: function (content) {
var style = this._style[document];
if (!style) {
style = this._style[document] = document.createElement('style');
document.getElementsByTagName('head')[0].appendChild(style);
};
style.styleSheet && (style.styleSheet.cssText += content) || style.appendChild(document.createTextNode(content));
},
_style: {},

// 獲取最終樣式
_getStyle: 'getComputedStyle' in window ? function (elem, name) {
return getComputedStyle(elem, null)[name];
} : function (elem, name) {
return elem.currentStyle[name];
},

// 獲取光標(biāo)在文本框的位置
_getFocus: function (elem) {
var index = 0;
if (document.selection) {// IE Support
elem.focus();
var Sel = document.selection.createRange();
if (elem.nodeName === 'TEXTAREA') {//textarea
var Sel2 = Sel.duplicate();
Sel2.moveToElementText(elem);
var index = -1;
while (Sel2.inRange(Sel)) {
Sel2.moveStart('character');
index++;
};
}
else if (elem.nodeName === 'INPUT') {// input
Sel.moveStart('character', -elem.value.length);
index = Sel.text.length;
}
}
else if (elem.selectionStart || elem.selectionStart == '0') { // Firefox support
index = elem.selectionStart;
}
return (index);
},

// 獲取元素在頁面中位置
_offset: function (elem) {
var box = elem.getBoundingClientRect(), doc = elem.ownerDocument, body = doc.body, docElem = doc.documentElement;
var clientTop = docElem.clientTop || body.clientTop || 0, clientLeft = docElem.clientLeft || body.clientLeft || 0;
var top = box.top + (self.pageYOffset || docElem.scrollTop) - clientTop, left = box.left + (self.pageXOffset || docElem.scrollLeft) - clientLeft;
return {
left: left,
top: top,
right: left + box.width,
bottom: top + box.height
};
}
};

function getPosition(ctrl) {
var p = kingwolfofsky.getInputPositon(ctrl);
document.getElementById('show').style.left = p.left + "px";
document.getElementById('show').style.top = p.bottom + "px";
}

測試頁面如下:


提示:可修改后代碼再運(yùn)行!

生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關(guān)閉
程序員人生
主站蜘蛛池模板: 成人av在线电影 | 免费在线看a | 亚洲精品电影在线观看 | 一级毛片在线播放 | 91麻豆精品国产91久久久使用方法 | 亚洲成人精品一区二区 | 精品久久久一区 | 成人免费视频观看 | 狠狠伊人 | 国产欧美日韩综合 | 一区免费视频 | 日韩精品成人在线观看 | 九九热在线视频观看这里只有精品 | 欧美日韩1区 | 91精品国产欧美一区二区成人 | 黄色小视频在线免费观看 | 久久久www成人免费精品 | 999久久 | 亚洲国产成人精品久久 | 日日夜夜亚洲 | 91麻豆产精品久久久久久 | 久久精品亚洲精品国产欧美 | 黄色小视频在线看 | 成人网18免费网站 | 国产精品电影在线观看 | 免费成人在线观看 | 天堂网中文在线 | 久久久噜噜噜久久中文字幕色伊伊 | 精品视频在线免费观看 | 国产偷久久一级精品60部 | 亚洲欧美在线综合 | 超碰2021| 成人免费在线观 | 综合国产 | 这里是精品 | 亚洲电影中文字幕 | 国产精品视频一区二区三区不卡 | 亚洲视频免费在线 | 日韩专区欧美专区 | 日韩高清在线免费观看 | 久久久久久久国产 |