Home > front end >  Div simulation text input box for length limit substr restrictions when word reached the cursor will
Div simulation text input box for length limit substr restrictions when word reached the cursor will

Time:09-29



Js code
Var=document. The textbox getElementById (' the userinfo),
G_selectNum=0,
Sel=window. GetSelection (),
The range=document. CreateRange ();
The function keyD (obj) {//press event
Var a=textbox. InnerHTML;

Var textA=textbox. The innerText. Substr (0, 20);
Range. SelectNodeContents (textbox);//will be added to the range content div node
Range. The collapse (false);
Sel. RemoveAllRanges ();
Sel. AddRange (range);//j will join the text field range object
}
The function keyPas () {
Var textA=textbox. The innerText. Substr (0, 20);
SetTimeout (function () {
Var a=textbox. InnerHTML;
Textbox. The innerText=a.r eplace (/& lt; Span. * "& gt; | & lt; \/span>/g, "");//will copy the content of span tags to replace
Range. SelectNodeContents (textbox);
Range. The collapse (false);
Sel. RemoveAllRanges ();
Sel. AddRange (range);
}, 300)
};
The function keyCop (obj) {
Var textA=textbox. The innerText. Substr (0, 20);
A_num=textA. Length;
Var selectCon=sel. ToString ();
G_selectNum=selectCon. Length;
}
The function keyAction (obj) {
Var textA=textbox. The innerText. Substr (0, 20);
A_num=textA. Length;
A_arr [1]=a_num;
Textbox. The innerText=textA;
Range. SelectNodeContents (textbox);
Range. The collapse (false);
Sel. RemoveAllRanges ();
Sel. AddRange (range);
}

CodePudding user response:

 & lt; Div id="TXT" contenteditable="true" border: 1 px solid # E1E1E1; Overflow: auto;"> 

Spare grass
CODE books
http://bbs.csdn.net/topics/390781214

CodePudding user response:

See there are many online about div analog input box do length limit. But they didn't mention use innerHTML length to judge the length and the innerHTML string assigned to div will appear when the cursor position, the default back to the first character that is I write wrong?? I want to solve the problem of the cursor position is also very thank you upstairs to the information provided I though I did not find the answer. Thanks

CodePudding user response:

But couldn't find a solution to think of this way when they arrive in maximum limit is to make the div out-of-focus have great spirit to solve the reply or notice of the

CodePudding user response:

The function set_focus (el)
{
El=el [0];//the jquery object dom object
El. The focus ();
If ($. Support. Msie)
{
Var range=document. Selection. CreateRange ();
This. The last=range;
Range. MoveToElementText (el);
Range. The select ();
Document. The selection. The empty ();//uncheck
}
The else
{
Var range=document. CreateRange ();
Range. SelectNodeContents (el);
Range. The collapse (false);
Var sel=window. GetSelection ();
Sel. RemoveAllRanges ();
Sel. AddRange (range);
}
}
So long, you must find a way, reply, the convenience of later time, this also is I found from other places, to find for a long time, where to find that I forgot to (embarrassing!) , such as illegal reprint, direct messages, delete immediately

CodePudding user response:

Now that is the analog input box, when the number of words to limit the number, directly to contenteditable attribute can be set to false, there is no need to bother!

CodePudding user response:

Now that is the analog input box, when the number of words to limit the number, directly to contenteditable attribute can be set to false, there is no need to bother!

CodePudding user response:

reference 4 floor ggwpgg1257 response:
function set_focus (el)
{
El=el [0];//the jquery object dom object
El. The focus ();
If ($. Support. Msie)
{
Var range=document. Selection. CreateRange ();
This. The last=range;
Range. MoveToElementText (el);
Range. The select ();
Document. The selection. The empty ();//uncheck
}
The else
{
Var range=document. CreateRange ();
Range. SelectNodeContents (el);
Range. The collapse (false);
Var sel=window. GetSelection ();
Sel. RemoveAllRanges ();
Sel. AddRange (range);
}
}
So long, you must find a way, reply, the convenience of later time, this also is I found from other places, to find for a long time, where to find that I forgot to (embarrassing!) , such as illegal reprint, DMS, would immediately delete


reference 4 floor ggwpgg1257 response:
function set_focus (el)
{
El=el [0];//the jquery object dom object
El. The focus ();
If ($. Support. Msie)
{
Var range=document. Selection. CreateRange ();
This. The last=range;
Range. MoveToElementText (el);
Range. The select ();
Document. The selection. The empty ();//uncheck
}
The else
{
Var range=document. CreateRange ();
Range. SelectNodeContents (el);
Range. The collapse (false);
Var sel=window. GetSelection ();
Sel. RemoveAllRanges ();
Sel. AddRange (range);
}
}
So long, you must find a way, reply, the convenience of later time, this also is I found from other places, to find for a long time, where to find that I forgot to (embarrassing!) , such as illegal reprint, DMS, would immediately delete

Thank you very much
  • Related