За последние 24 часа нас посетили 18296 программистов и 1617 роботов. Сейчас ищут 1906 программистов ...

Поиск и замена выделенного текста

Тема в разделе "JavaScript и AJAX", создана пользователем Nazorei, 9 авг 2009.

  1. Nazorei

    Nazorei Активный пользователь

    С нами с:
    1 фев 2007
    Сообщения:
    228
    Симпатии:
    0
    Может у кого имеется подобная функция, буду благодарен..

    суть функции:
    передается 3 параметра
    1 - ид элемента textarea (где будет производиться поиск)
    2 и 3 - по краям найденного текста добавляем значения этих параметров,

    например,
    Код (Text):
    1. myfunc('id_txarea','[b]','[/b]')
    текст для поиска - текущий выделенный текст в теге textarea с ид id_txarea
     
  2. Kreker

    Kreker Старожил

    С нами с:
    8 апр 2007
    Сообщения:
    5.433
    Симпатии:
    0
  3. Mr.M.I.T.

    Mr.M.I.T. Старожил

    С нами с:
    28 янв 2008
    Сообщения:
    4.586
    Симпатии:
    1
    Адрес:
    у тебя канфетка?
  4. Nazorei

    Nazorei Активный пользователь

    С нами с:
    1 фев 2007
    Сообщения:
    228
    Симпатии:
    0
    Kreker
    спасибо

    Mr.M.I.T.
    слишком много лишнего..


    В общем сделал так, вроде работает как надо по условию задачи (ie,ff,opera):

    Код (Text):
    1. function paste_str(id,bb1,bb2)
    2. {
    3.  var str; var id=document.getElementById(id);
    4.  if (id) if (document.selection)
    5.  {
    6.   id.focus();
    7.   sel = document.selection.createRange();
    8.   sel.text = bb1 + sel.text + bb2;
    9.   sel.select();
    10.   return;
    11.  } else
    12.  {
    13.   var startPos = id.selectionStart; var endPos = id.selectionEnd;
    14.   str = id.value.substring(startPos, endPos);
    15.   id.value = id.value.substring(0, startPos) + bb1 + str + bb2 + id.value.substring(endPos, id.value.length);
    16.   return;
    17.  }
    18. }
     
  5. Kreker

    Kreker Старожил

    С нами с:
    8 апр 2007
    Сообщения:
    5.433
    Симпатии:
    0
    Посмотри, как реализовано в форумной функции - там 6 строк что-ли.
     
  6. Mr.M.I.T.

    Mr.M.I.T. Старожил

    С нами с:
    28 янв 2008
    Сообщения:
    4.586
    Симпатии:
    1
    Адрес:
    у тебя канфетка?
    [js]function bbstyle(bbnumber) {
    var txtarea = document.post.message;

    txtarea.focus();
    donotinsert = false;
    theSelection = false;
    bblast = 0;

    if (bbnumber == -1) { // Close all open tags & default button names
    while (bbcode[0]) {
    butnumber = arraypop(bbcode) - 1;
    txtarea.value += bbtags[butnumber + 1];
    buttext = eval('document.post.addbbcode' + butnumber + '.value');
    eval('document.post.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
    }
    imageTag = false; // All tags are closed including image tags :D
    txtarea.focus();
    return;
    }

    if ((clientVer >= 4) && is_ie && is_win)
    {
    theSelection = document.selection.createRange().text; // Get text selection
    if (theSelection) {
    // Add tags around selection
    document.selection.createRange().text = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1];
    txtarea.focus();
    theSelection = '';
    return;
    }
    }
    else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0))
    {
    mozWrap(txtarea, bbtags[bbnumber], bbtags[bbnumber+1]);
    return;
    }

    // Find last occurance of an open tag the same as the one just clicked
    for (i = 0; i < bbcode.length; i++) {
    if (bbcode == bbnumber+1) {
    bblast = i;
    donotinsert = true;
    }
    }

    if (donotinsert) { // Close all open tags up to the one just clicked & default button names
    while (bbcode[bblast]) {
    butnumber = arraypop(bbcode) - 1;
    txtarea.value += bbtags[butnumber + 1];
    buttext = eval('document.post.addbbcode' + butnumber + '.value');
    eval('document.post.addbbcode' + butnumber + '.value ="' + buttext.substr(0,(buttext.length - 1)) + '"');
    imageTag = false;
    }
    txtarea.focus();
    return;
    } else { // Open tags

    if (imageTag && (bbnumber != 14)) { // Close image tag before adding another
    txtarea.value += bbtags[15];
    lastValue = arraypop(bbcode) - 1; // Remove the close image tag from the list
    document.post.addbbcode14.value = "Img"; // Return button back to normal state
    imageTag = false;
    }

    // Open tag
    txtarea.value += bbtags[bbnumber];
    if ((bbnumber == 14) && (imageTag == false)) imageTag = 1; // Check to stop additional tags after an unclosed image tag
    arraypush(bbcode,bbnumber+1);
    eval('document.post.addbbcode'+bbnumber+'.value += "*"');
    txtarea.focus();
    return;
    }
    storeCaret(txtarea);
    }[/js]
     
  7. Mr.M.I.T.

    Mr.M.I.T. Старожил

    С нами с:
    28 янв 2008
    Сообщения:
    4.586
    Симпатии:
    1
    Адрес:
    у тебя канфетка?
    это ещё не всё