网上苦找2小时,全是不能兼容FF的,看来这种东西网上是搞不到现成的了,只能自己动手丰衣足食 现在发布出来,今后网上就有现成的供人使用了。 为了省事,少量位置用了jquery 改原生JS也很方便,谁需要就请自己修改了。 欢迎各位路过高人拍板,欢迎各位留言提供改进代码。 又改进 兼容了Chrome 下面的代码已修改成最新版 上源码了 复制代码 代码如下: js获取div编辑框,textarea,input text的光标位置,兼容FF和IE <br>//去除左右所有空格 <br>String.prototype.trim = function () { <br>return this.replace(/(^s*)|(s*$)/g, ""); <br>} <br>function getPosition(element) { <br>var OsObject = ""; <br>if (navigator.userAgent.indexOf("MSIE") > 0) { <br>OsObject = "MSIE"; <br>} <br>if (navigator.userAgent.indexOf("Firefox") > 0) { <br>OsObject = "Firefox"; <br>} <br>if (navigator.userAgent.indexOf("Safari") > 0) { <br>OsObject = "Safari"; <br>} <br>if (navigator.userAgent.indexOf("Camino") > 0) { <br>OsObject = "Camino"; <br>} <br>if (navigator.userAgent.indexOf("Gecko") > 0) { <br>OsObject = "Gecko"; <br>} <br>if (navigator.userAgent.indexOf("Chrome") > 0) { <br>OsObject = "Chrome"; <br>} <br><br>var result = 0; <br>if (!document.selection) { //非IE浏览器 <br>var thisTagName = null; <br>if ($(element).attr("tagName") != "TEXTAREA" && $(element).attr("tagName") != "INPUT") { <br>if ($(element).attr("tagName") == "DIV" && $(element).attr("contenteditable") == "true") { <br>thisTagName = window.getSelection().anchorNode.parentElement.tagName; <br>} //出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢! <br>else { <br>thisTagName == null; <br>} <br>} <br>else { <br>if ($(element).attr("tagName") == "INPUT" && $(element).attr("type") == "text") { <br>thisTagName = window.getSelection().anchorNode.tagName; <br>} <br>else { <br>console.log(window.getSelection()); <br>thisTagName = window.getSelection().anchorNode.tagName; <br>} <br>} <br>console.log(thisTagName); <br>if (thisTagName == "TEXTAREA" || thisTagName == "INPUT" || (thisTagName=="BODY"&&OsObject == "Chrome")) { <br>result = element.selectionStart <br>} //出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢! <br>else if (thisTagName != null) { <br>if (thisTagName == element.tagName) { <br>if (window.getSelection().anchorNode.textContent == $(element).text()) { <br>result = window.getSelection().anchorOffset; <br>} <br>else { <br>var currentIndex = window.getSelection().anchorOffset; <br>var txt = ""; <br>var txtoo = window.getSelection().anchorNode.previousSibling; <br>while (txtoo != null) { <br>txt = txtoo.textContent; <br>txtoo = txtoo.previousSibling; <br>} //出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢! <br>result = txt.trim().length currentIndex; <br>} <br>} <br>else { <br>var currentIndex = window.getSelection().anchorOffset; <br>var txt = ""; <br>var txtoo = window.getSelection().anchorNode.parentElement.previousSibling; <br>while (txtoo != null) { <br>txt = txtoo.textContent; <br>txtoo = txtoo.previousSibling; <br>} <br>result = txt.trim().length currentIndex; <br>} <br>} //出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢! <br>else { <br>return 0; <br>} <br>} else { //IE <br>var rng; <br>if ($(element).attr("tagName") == "TEXTAREA" || ($(element).attr("tagName") == "INPUT" && $(element).attr("type") == "text") || ($(element).attr("tagName") == "DIV" && $(element).attr("contenteditable") == "true")) { <br>element.focus(); <br>rng = document.selection.createRange(); <br>rng.moveStart('character', -element.innerText.length); <br>var text = rng.text; <br>for (var i = 0; i < element.innerText.length; i ) { <br>if (element.innerText.substring(0, i 1) == text.substring(text.length - i - 1, text.length)) { <br>result = i 1; <br>} <br>} <br>} //出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢! <br>else { <br>return 0; <br>} <br>} <br>return result; <br>} <br><br>function getValue(element) { <br>var pos = getPosition(element); <br>document.getElementById("pnum").value = pos; <br>} <br><br> <br>#Div1, #Div2 <br>{ <br>width: 500px; <br>height: 100px; <br>border: solid 1px black; <br>} <br> style="display: block" /> Hello,wellcome to test! 你好,欢迎测试! 注意源代码开闭合标记之间不能换行,否则统计错误! 一二三四五六七八九零 注意源代码开闭合标记之间不能换行,否则统计错误!注意div编辑框源码里面是嵌套有其他标签的,可以正常返回正确位置! 一二三四五六七八