Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Textarea
Forum PHP.pl > Forum > Po stronie przeglądarki > JavaScript
Widmo9
Witam mam taki kod do wstawiania znacznikow do textarea:
  1. <script type="text/javascript">
  2. <!--
  3.  
  4. /**************************************************************************/
  5.  
  6. function closeTags(str, tagsToClose) {
  7. var tags = {}, ret = '', sT = tagsToClose;
  8. str.replace(/<(\/)?([\w]+)([\s=][^<>]*)?>/g, function(a, b, c) {
  9. if (isNaN(tags[c = c.toLowerCase()])) tags[c] = 0;
  10. for (var i = 0; i < sT.length;) if (c == sT[i++]) {
  11. b != '/' ? tags[c]++ : tags[c] > 0 ? tags[c]-- : 0; break;
  12. }
  13. } );
  14. for (var v in tags) while (tags[v]--) ret += '[/' + v + ']';
  15. return ret;
  16. }
  17.  
  18. function selExist(elm) {
  19. if (elm.setSelectionRange) {
  20. return elm.selectionStart != elm.selectionEnd;
  21. }
  22. else if (document.selection) {
  23. elm.focus();
  24. var range = document.selection.createRange();
  25. return range.text != '';
  26. }
  27. return false;
  28. }
  29.  
  30. function addToEnd(elm, text) {
  31. elm.focus();
  32. elm.value += text;
  33. if (elm.setSelectionRange) {
  34. var len = elm.value.length;
  35. elm.setSelectionRange(len, len);
  36. }
  37. elm.scrollTop = elm.scrollHeight;
  38. }
  39.  
  40. function selReplace(elm, text1, text2) {
  41. elm.focus();
  42. if (elm.setSelectionRange) {
  43. if (typeof text2 != 'undefined') {
  44. text1 += elm.value.substring(elm.selectionStart, elm.selectionEnd) + text2;
  45. }
  46. var scrollPos = [elm.scrollLeft, elm.scrollTop];
  47. var begin = elm.value.substr(0, elm.selectionStart);
  48. var end = elm.value.substr(elm.selectionEnd);
  49. var curPos = elm.selectionStart + text1.length;
  50. elm.value = begin + text1 + end;
  51. elm.setSelectionRange(curPos, curPos);
  52. elm.scrollLeft = scrollPos[0];
  53. elm.scrollTop = scrollPos[1];
  54. }
  55. else if (document.selection) {
  56. var range = document.selection.createRange();
  57. if (typeof text2 != 'undefined') {
  58. text1 += range.text + text2;
  59. }
  60. range.text = text1;
  61. range.select();
  62. }
  63. else {
  64. if (typeof text2 != 'undefined') text1 += text2;
  65. addToEnd(elm, text1);
  66. }
  67. }
  68.  
  69. /**************************************************************************/
  70.  
  71. var tagList = ['b', 'i', 'u', 'code', 'img'];
  72.  
  73. var tagOpened = [];
  74.  
  75. function insertText(text) {
  76. var elm = document.forms.my_form.my_textarea;
  77. selReplace(elm, ' ' + text + ' ');
  78. }
  79.  
  80. function pasteTag(nmbr, btn) {
  81. var elm = document.forms.my_form.my_textarea;
  82. if (selExist(elm)) {
  83. selReplace(elm, '[' + tagList[nmbr] + ']', '[/' + tagList[nmbr] + ']');
  84. }
  85. else {
  86. if (tagOpened[nmbr]) {
  87. selReplace(elm, '[/' + tagList[nmbr] + ']');
  88. btn.value = btn.value.substr(0, btn.value.length - 1);
  89. tagOpened[nmbr] = false;
  90. }
  91. else {
  92. selReplace(elm, '[' + tagList[nmbr] + ']');
  93. btn.value += '*';
  94. tagOpened[nmbr] = true;
  95. }
  96. }
  97. }
  98.  
  99. function closeAllTags() {
  100. var elm = document.forms.my_form.my_textarea;
  101. var inpts = document.forms.my_form.elements;
  102. var tmpStr = closeTags(elm.value, tagList);
  103. if (tmpStr != '') {
  104. addToEnd(elm, tmpStr);
  105. tagOpened = [];
  106. for (var i = 0; i < inpts.length; i++) {
  107. if (inpts[i].type.toLowerCase() == 'button') {
  108. with (inpts[i].value) if (charAt(length - 1) == '*') {
  109. inpts[i].value = inpts[i].value.substr(0, inpts[i].value.length - 1);
  110. }
  111. }
  112. }
  113. }
  114. elm.focus();
  115. }
  116.  
  117. //-->


i takimi buttonami je wstawiam:
  1. <input class="btn1" type='button' value='B' accesskey="b" onclick="pasteTag(0, this)" onmouseover="overlib('<div align=center>pogrubienie</div>')" onmouseout="nd();"/>
  2. <input class="btn1" type='button' value='I' accesskey="i" onclick="pasteTag(1, this)" onmouseover="overlib('<div align=center>kursywa</div>')" onmouseout="nd();"/>
  3. <input class="btn1" type='button' value='U' accesskey="u" onclick="pasteTag(2, this)" onmouseover="overlib('<div align=center>podkreślenie</div>')" onmouseout="nd();"/>
  4. <input class="btn1" type='button' value='CODE' accesskey="c" onclick="pasteTag(3, this)" onmouseover="overlib('<div align=center>kod źródłowy</div>')" onmouseout="nd();"/>
  5. <input class="btn1" type='button' value='IMG' accesskey="m" onclick="pasteTag(4, this)" onmouseover="overlib('<div align=center>wstaw obrazek (wpisz nazwe z rozszerzeniem)</div>')" onmouseout="nd();"/>
  6. <input class="btn1" type="button" onclick="closeAllTags()" value="Zamknij" onmouseover="overlib('<div align=center>Zamknij znaczniki</div>')" onmouseout="nd();">
  7. <TEXTAREA NAME="my_textarea" COLS="60" ROWS="15"></TEXTAREA>

I tu pojawia sie problem ponieważ button zamknij znaczniki wogule ich nie zamyka. Prosze o pomoc
nospor
Przenoszę na JavaScript
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.