A div editable box. I need to save the contents of the input box as the first ten characters when the input character is greater than 10. The following code is fine in Google browser, but when I type "11111111111" in IE8, it can be intercepted normally, but the result displayed by typing "ah" will become an "ah".
wait online, solutions, and I"m not sure where the problem is.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
</head>
<body>
<div id="myDiv" contenteditable="true" style="width: 100%; height: 80px; border: 1px solid -sharpCCC;" onblur="myDivCalc()" onkeyup="myDivCalc()"></div>
<script>
function myDivCalc(){
var str = $("-sharpmyDiv").text();
if(str.length<=10){
}else{
var strJ = str.substring(0,10);
$("-sharpmyDiv").text(strJ);
set_focus(document.getElementById("myDiv"))
}
}
//div
function set_focus(el){
//el=el[0]; //jquery dom
el.focus();
if($.support.msie)
{
var range = document.selection.createRange();
this.last = range;
range.moveToElementText(el);
range.select();
document.selection.empty(); //
}
else
{
var range = document.createRange();
range.selectNodeContents(el);
range.collapse(false);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
}
</script>
</body>
</html>