![]() |
![]() ![]() |
![]() |
![]()
Post
#1
|
|
![]() Grupa: Zarejestrowani Postów: 623 Pomógł: 11 Dołączył: 1.01.2009 Skąd: Wrocław Ostrzeżenie: (0%) ![]() ![]() |
Chciałem stworzyć prosty edytor tekstu na forum. Mam coś takiego:
w head: Kod <script language=\"javascript\"> var ilei=0; function wstaw(litera) { if(litera == 'i'){ if(ilei==0){ document.getElementById('text').value += "[ " + litera + " ]"; ilei++; }else{ document.getElementById('text').value += "[ /" + litera + " ]"; ilei--; } } } </script> w body: Kod <form id="formularz" action="dodaj.php" method="post"> <a href="javascript:void(0)" onclick="wstaw('i');">i</a> <textarea id="text" rows="10" name="odpowiedz" cols="163">$odpowiedz<textarea> </form> Gdy klikam w " i " to w polu tekstowym za teksem pojawia się " [ i ] " lub "[ /i ]". Ale ja chciałbym, żeby pojawiało się w miejscu gdzie jest kursor. Jak to zrobić? -------------------- |
|
|
![]()
Post
#2
|
|
![]() Grupa: Zarejestrowani Postów: 603 Pomógł: 131 Dołączył: 24.07.2007 Skąd: Górny Śląsk Ostrzeżenie: (0%) ![]() ![]() |
-------------------- |
|
|
![]()
Post
#3
|
|
![]() Grupa: Zarejestrowani Postów: 623 Pomógł: 11 Dołączył: 1.01.2009 Skąd: Wrocław Ostrzeżenie: (0%) ![]() ![]() |
Mam coś takiego:
W HEADzie: Kod <script type="text/javascript"> ilei = 0; ileb = 0; ileu = 0; function formatText(el,tag){ var selectedText = document.selection?document.selection.createRange().text:el.value.substring(el.selectionStart,el.selectionEnd); if(selectedText!=''){ var newText='<'+tag+'>'+selectedText+'</'+tag+'>'; el.value=el.value.replace(selectedText,newText); }else{ var newText='<'+tag+'>'; if(tag=='i'){ if(ilei==0){ document.getElementById('myTextarea').value += "<" + tag + ">"; ilei++; }else{ document.getElementById('myTextarea').value += "</" + tag + ">"; ilei--; } } if(tag=='b'){ if(ileb==0){ document.getElementById('myTextarea').value += "<" + tag + ">"; ileb++; }else{ document.getElementById('myTextarea').value += "</" + tag + ">"; ileb--; } } if(tag=='u'){ if(ileu==0){ document.getElementById('myTextarea').value += "<" + tag + ">"; ileu++; }else{ document.getElementById('myTextarea').value += "</" + tag + ">"; ileu--; } } } } </script> W BODY: Kod <form action="" id="myForm"> <textarea name="myTextarea" id="myTextarea" rows="12" cols="50">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</textarea><br> <input type="button" value="Bold" onclick="formatText(myTextarea,'b')"> <input type="button" value="Italic" onclick="formatText(myTextarea,'i')"> <input type="button" value="Underline" onclick="formatText(myTextarea,'u')"> </form> Ale nadal nie chce mi się pojawiać znacznik w miejscu kursora ![]() Ten post edytował sweter 9.02.2009, 12:31:20 -------------------- |
|
|
![]()
Post
#4
|
|
![]() Grupa: Zarejestrowani Postów: 603 Pomógł: 131 Dołączył: 24.07.2007 Skąd: Górny Śląsk Ostrzeżenie: (0%) ![]() ![]() |
Przetestowałem. Wstawia gdzie trzeba, jeśli zaznaczony ciąg jest unikalny. Problem pojawia się jeśli np. mamy dwa razy słowo "xx" i zaznaczymy drugie. Wtedy zaznaczy pierwsze wystąpienie. Jedyne rozwiązanie jakie mi przychodzi do głowy to znaleźć początek zaznaczenia i koniec po czym korzystając z substring() posklejać doklejając znaczniki. Problemem jest znalezienie początku i końca zaznaczenia (przynajmniej)w IE.
1. Albo zrób jak napisałem powyżej; google może Ci pomoże znaleźć granice zaznaczenia. 2. Podglądnij jakiś edytor np. ten tu na forum. -------------------- |
|
|
![]() ![]() |
![]() |
Aktualny czas: 21.08.2025 - 02:15 |