網路上苦找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") > 00 ) { <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 = ""; txtoo = window.getSelection().anchorNode.previousSibling; <BR>while (txtoo != null) { <BR>txt = txtoo.textContent; <BR>txtoo = txtoo.previousSibling; <http🎜>; //www.cnblogs.com/ahjesus 尊重作者辛苦勞動成果,轉載請註明出處,謝謝! <BR>result = txt.trim().length currentIndex; <BR>} <BR>} <BR>else { <BR>var currentIndex = window.getSelection().anchorOset; <BR> <BR>var txtoo = window.getSelection().anchorNode.parentElement.previousSibling; <BR>while (txtoo != null) { <BR>txt = txtoo.textContent; <BR>txtoo = txtoo. } <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. ); <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編輯框原始碼裡面是巢狀有其他標籤的,可以正常回傳正確位置! 一二三四五六七八