Get div edit box, textarea,input text cursor position compatible IE,FF and Chrome method Introduction _javascript Tips

Source: Internet
Author: User
Online hard to find 2 hours, all is not compatible with FF, it seems this kind of things online is not ready, only to do their own food and clothing
Now released, there will be available on the Internet for people to use.

In order to save trouble, a small number of locations with the jquery to change the original JS is also very convenient, who need to please change their own.
Welcome you to pass the high man, welcome your message to provide improved code.
and improved compatibility with chrome the following code has been modified to the latest version
On the source.
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title>js gets the div edit box, textarea,input text cursor position, compatible FF and ie</title>
<script src= "Jquery-1.4.4.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
Remove all space left and right
String.prototype.trim = function () {
Return This.replace (/(^\s*) | ( \s*$)/g, "");
}
function GetPosition (Element) {
var osobject = "";
if (Navigator.userAgent.indexOf ("MSIE") > 0) {
Osobject = "MSIE";
}
if (Navigator.userAgent.indexOf ("Firefox") > 0) {
Osobject = "Firefox";
}
if (Navigator.userAgent.indexOf ("Safari") > 0) {
Osobject = "Safari";
}
if (Navigator.userAgent.indexOf ("Camino") > 0) {
Osobject = "Camino";
}
if (Navigator.userAgent.indexOf ("Gecko") > 0) {
Osobject = "Gecko";
}
if (Navigator.userAgent.indexOf ("Chrome") > 0) {
Osobject = "Chrome";
}

var result = 0;
if (!document.selection) {//Non IE browser
var thistagname = null;
if (Element). attr ("TagName")!= "TEXTAREA" && $ (Element). attr ("TagName")!= "INPUT" {
if (Element). attr ("tagName") = = "DIV" && $ (Element). attr ("contenteditable") = = "true") {
Thistagname = Window.getselection (). AnchorNode.parentElement.tagName;
//From Http://www.cnblogs.com/ahjesus respect the author's hard work, reproduced please indicate the source, thank you!
else {
Thistagname = null;
}
}
else {
if (Element). attr ("tagName") = = "INPUT" && $ (Element). attr ("type") = = "Text") {
Thistagname = Window.getselection (). Anchornode.tagname;
}
else {
Console.log (Window.getselection ());
Thistagname = Window.getselection (). Anchornode.tagname;
}
}
Console.log (Thistagname);
if (Thistagname = = "TEXTAREA" | | thistagname = = "INPUT" | | (thistagname== "body" &&osobject = = "Chrome")) {
result = Element.selectionstart
//From Http://www.cnblogs.com/ahjesus respect the author's hard work, reproduced please indicate the source, thank you!
else if (thistagname!= null) {
if (Thistagname = = Element.tagname) {
if (Window.getselection (). Anchornode.textcontent = = $ (Element). Text ()) {
result = Window.getselection (). Anchoroffset;
}
else {
var currentindex = Window.getselection (). Anchoroffset;
var txt = "";
var Txtoo = Window.getselection (). anchornode.previoussibling;
while (Txtoo!= null) {
TXT + + txtoo.textcontent;
Txtoo = txtoo.previoussibling;
//From Http://www.cnblogs.com/ahjesus respect the author's hard work, reproduced please indicate the source, thank you!
result = Txt.trim (). length + currentindex;
}
}
else {
var currentindex = Window.getselection (). Anchoroffset;
var txt = "";
var Txtoo = Window.getselection (). anchorNode.parentElement.previousSibling;
while (Txtoo!= null) {
TXT + + txtoo.textcontent;
Txtoo = txtoo.previoussibling;
}
result = Txt.trim (). length + currentindex;
}
//From Http://www.cnblogs.com/ahjesus respect the author's hard work, reproduced please indicate the source, thank you!
else {
return 0;
}
else {//ie
var rng;
if ($ (element). attr ("tagName") = = "TEXTAREA" | | (Element). attr ("tagName") = = "INPUT" && $ (Element). attr ("type") = = "text") | | ($ (Element). attr ("tagName") = = "DIV" && $ (Element). attr ("contenteditable") = = "true") {
Element.focus ();
RNG = Document.selection.createRange ();
Rng.movestart (' character ',-element.innertext.length);
var text = Rng.text;
for (var i = 0; i < element.innerText.length; i++) {
if (element.innerText.substring (0, i + 1) = = Text.substring (text.length-i-1, text.length)) {
result = i + 1;
}
}
//From Http://www.cnblogs.com/ahjesus respect the author's hard work, reproduced please indicate the source, thank you!
else {
return 0;
}
}
return result;
}

function GetValue (Element) {
var pos = getPosition (Element);
document.getElementById ("Pnum"). Value = pos;
}

</script>
<style type= "Text/css" >
#Div1, #Div2
{
width:500px;
height:100px;
Border:solid 1px black;
}
</style>
<body>
<input id= "Pnum" type= "text" value= "Hello,wellcome to test! Hello, Welcome to test! "onmouseup=" GetValue (This) "
style= "Display:block"/>
<textarea cols= "rows=" onkeyup= "GetValue (This)" onmouseup= "GetValue (a)" >hello,wellcome to test! Hello, Welcome to the test! Note that the source code open close tags can not be wrapped, or statistical error! </textarea>
<div id= "Div1" onclick= "getValue (this);" onkeyup= "getValue (this);" contenteditable= "true" > 123 <span> four or five </span> 678 <span> 90 </span> Note The source code open close mark can not be wrapped, or statistical error! Note div edit box inside the source is nested with other tags, you can return to the correct position correctly! </div>
<br/>
<div id= "Div2" onclick= "GetValue" (this); "onkeyup=" getValue (this); " > 123456 <span> seven or eight </span></div>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.