inputTextArea在IE8中失去焦点

时间:2013-03-28 06:27:17

标签: javascript

我有一个jsf 2.0应用程序,其中输入文本框和输入文本框底部的数字字符左计数器,我通过javascript更新。

<h:inputTextarea class="myInputTextClass"
value="#{bean.text}"
style="resize: none;width:445px;" type="text"
maxlength="255" cols="60" rows="3"
onfocus="countChars($('.myInputTextClass'),255)"
onkeyup="countChars($('.myInputTextClass'),255);"
onkeydown="countChars($('.myInputTextClass'),255);"
/>

<span id="char_count" style="font-style:italic;margin-left:-14px">255</span>

脚本:

function countChars(element,  max) {

  calculateCount(element.val().length, max);
  if (element.val().length > max) { 
    element.val(element.val().substring(0, max));
   }

  }

function calculateCount(length, max){
 var count = max - length;
    if(count &lt; 0){
    count = 0;
    }
   document.getElementById('char_count').innerHTML =  count ;
}

问题是在输入文本框中输入大约150个字符后,焦点会移回到inputtextbox的顶部。但是,光标仍然按预期结束。

我尝试使用scrollTop位置和焦点但不起作用。任何建议都受到高度赞赏。

这个问题只发生在IE8中,在forefox中运行正常。

1 个答案:

答案 0 :(得分:0)

这可能不是您正在寻找的答案,但多年来这个脚本对我很有帮助:

JavaScript的:

/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Steve | http://jsmadeeasy.com/ 
http://www.javascriptsource.com/forms/character-counter.html */
function getObject(obj) {
  var theObj;
  if(document.all) {
   if(typeof obj=="string") {
     return document.all(obj);
   } else {
     return obj.style;
   }
 }
 if(document.getElementById) {
   if(typeof obj=="string") {
     return document.getElementById(obj);
   } else {
     return obj.style;
   }
 }
 return null;
}

function toCount(entrance,exit,text,characters) {
 var entranceObj=getObject(entrance);
 var exitObj=getObject(exit);
 var length=characters - entranceObj.value.length;
 if(length <= 0) {
   length=0;
   text='<span class="disable"> '+text+' </span>';
   entranceObj.value=entranceObj.value.substr(0,characters);
 }
 exitObj.innerHTML = text.replace("{CHAR}",length);
}

HTML&amp; PHP:

(<span id="sBann">'.(255 - strlen($values['synopsis'])).' characters left</span>)<br />
<textarea name="synopsis" id="synopsis" rows="3" cols="70" maxlength="255" wrap="soft" onkeyup="toCount(\'synopsis\',\'sBann\',\'{CHAR} characters left\',255);">'.htmlentities($values['synopsis']).'</textarea>