Программирование > JavaScript & JScript

позици курсора и textarea

(1/1)

Greg:
требуется стандартная функция: вставка текст в любое место 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:

/**
 * Обрамляет выделение в поле с ИД=fieldId формы с NAME=formName
 * текстами text1, text2
 */
function inpos(text1, text2, fieldId, formName) {
  var field = document.getElementById(fieldId)
  var f = 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;
  }
}

Навигация

[0] Главная страница сообщений

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 
Перейти к полной версии