Автор Тема: позици курсора и textarea  (Прочитано 2985 раз)

0 Пользователей и 1 Гость просматривают эту тему.

Оффлайн Greg

  • пинёг
  • Ветеран
  • *****
  • Сообщений: 618
  • +0/-0
  • 2
    • Просмотр профиля
    • http://www.bsfera.ru
позици курсора и textarea
« : 07 Сентября 2007, 14:17:16 »
требуется стандартная функция: вставка текст в любое место textarea, а не только в конец текста там присутствующего. пробовал так:
<script language="Javascript">
function 
insert_code(code)
{
var 
text document.form.document.selection.createRange().text;
document.form.main_code.focus();
document.form.document.selection.createRange().text code+text;
document.form.main_code.focus();
}
</script>

в файрфоксе не работает. нужно универсальное решения. кто знает?
Пространные размышления об ИТ и все что вокруг

Оффлайн andymc

  • Фанат форума
  • Старожил
  • ****
  • Сообщений: 400
  • +1/-0
  • 0
    • Просмотр профиля
позици курсора и textarea
« Ответ #1 : 07 Сентября 2007, 15:03:22 »

/**
 * Обрамляет выделение в поле с ИД=fieldId формы с NAME=formName
 * текстами text1, text2
 */
function inpos(text1text2fieldIdformName) {
  var 
field document.getElementById(fieldId)
  var 
document.getElementsByName(formName)
  var 
forma f[0]
  
// field.focus(); с фокусом в NN потом курсор в конец улетает
  
!text2 text2 = \'\' : true;
  // для NN
  var decrement = 0
  if ( ! document.all && ! field.selectionStart){
    field.selectionStart = 1
    decrement = 1
  }
  if (field.selectionStart){
    var val = field.value
    var val1 = val.substring(0, field.selectionStart - decrement)
    var val2 = val.substring(field.selectionStart - decrement, field.selectionEnd)
    var val3 = val.substring(field.selectionEnd, val.length)
    field.value = val1 + text1 + val2 + text2 + val3
    return;
  }
  // для IE
  if (document.selection) {
    var sText = forma.document.selection.createRange().text;
    if (!sText) {
      field.focus();
    }
    forma.document.selection.createRange().text = text1 + sText + text2;
  // для прочих
  } else {
    field.value = field.value + text1  + text2;
  }
}

 

Sitemap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28